2

I'm working on a program for my Java class which needs to calculate how many children a couple would need to have in order to have one of each sex, assuming its a 50/50 chance that the child will be a male. The program then needs to keep track of how many times it took 2 children to have one of each sex, how many times it took 3 children, 4 children, and finally 5 or more children in T amount of trials. I tackled this by nesting a while loop within a for loop that runs T amount of times. My problem is that, although my for loop is running T amount of times, my while loop will spit out, say, 3 children and then not update for the rest of the for loop. Any advice as to how to get the while loop to update properly? Thanks!

public class B
{
    public static void main(String[] args) 
    {
        int girls = 0;
        int boys = 0;
        System.out.print("Enter variable: ");
        int T = StdIn.readInt();

        for (int i = 0; i < T; i++)
        {
            while ((boys < 1) || (girls < 1))
            {
                if (Math.random() < 0.5)
                {
                    boys = boys + 1;
                }   
                else
                {
                    girls = girls + 1;
                }   
            }
        }
    }
}
1
  • Your code is doing exactly what you asked it to do. I fear that you don't understand the problem statement correctly. Commented Sep 9, 2015 at 0:43

1 Answer 1

3

You need to reset the boy and girl count each run. Therefore the code around your while loop should be:

boys = 0;
girls = 0;
while ((boys < 1) || (girls < 1))

But then after the while loop, you need to calculate which family size this run applies to: 2 kids, 3 kids, 4 kids, 5+ kids and increment the appropriate counter.

switch(boys + girls) {
    case 2:
        twoKids++;
    break;

    case 3:
        threeKids++;
    break;

    case 4:
        fourKids++;
    break;

    default:
        fivePlusKids++;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thats what I was thinking too, but running my program with && rather than || does not produce the proper output. If I run the program outside of the for loop and add in some code to print out how many boys and girls there are, it works correctly with || and produces things like girls: 0 boys: 2 with &&. I just need a way to make my while loop repeat so that i get, say, 10 separate outpost from it.
You're right - the problem isn't in the while clause, it is because you needed to reset the count of boys and girls. I've edited the answer.
What if you want to track families with 10+ kids? The switch case solution doesn't seem to scale for that. An array seems more appropriate.
The OP never requested that. But it could be done by storing the result in a map keyed by the number of children. An array would not scale well either.
@Jason One array to maintain state seems preferable to N counters. Initialize the array to the number of children you want to track and each element represents the count for N kids.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.