1. Member
Join Date
Sep 2014
Posts
1
Rep Power
0

## Predicting the gender of a baby HELP PLEASE

Hello everyone,

I am doing a project where I'm supposed to say how many children a couple has to have to have both a boy and a girl. It is given that the probability is 50/50I am very stuck.

This is what I have so far but I can't figure out how to have it check whether the Math.random has been both < 0.5 and >0.5.

public class BoyAndGirl
{
public static void main(String [] args)
{
int boys = 0;
int girls = 0;
int i = 1;

while (boys != 1,girls != 1)
{

i++;
int x;
if (Math.random() < 0.5) x = boys;

else x = girls;

}
System.out.println("Congratulations! You have " + i + " children.");

}

}

2. ## Re: Predicting the gender of a baby HELP PLEASE

What is the condition in that 'while( ... )' statement trying to express? That is not Java; say it in words and we'll try to translate it to proper Java. Oh and stick your code between [code] ... [/code] tags, so the indentation of your code is preserved.

kind regards

Jos

ps. I'm moving this thread to the 'New To Java' forum.

3. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: Predicting the gender of a baby HELP PLEASE

Jos has mentioned the slightly curious while syntax, but you also never set either the 'boys' or the 'girls' variables, so they are always 0.
And what's 'x' for?

I think you need to (as Jos says) write out in words what your logic is going to be, then you can work on translating that into code.

4. Member
Join Date
Sep 2014
Posts
2
Rep Power
0

## Re: Predicting the gender of a baby HELP PLEASE

First, this is not a valid boolean expression:
Java Code:
`while (boys != 1,girls != 1)`
you don't use a comma, you use double ampersand.
the probability expression is correct, but instead of using the useless variable of 'x', you just have to increase both, boys and girls count (++).
also 'i' variable is useless since it's the value of boys plus girls, if you want to use it, you can assign it's value outside of the while loop.

5. ## Re: Predicting the gender of a baby HELP PLEASE

Originally Posted by Decimal314
First, this is not a valid boolean expression:
Java Code:
`while (boys != 1,girls != 1)`
you don't use a comma, you use double ampersand.
That would result in a syntactically correct, but semantically incorrect expression.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•