Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Predicting the gender of a baby HELP PLEASE

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

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default 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.
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Decimal314 is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default 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. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Predicting the gender of a baby HELP PLEASE

    Quote Originally Posted by Decimal314 View Post
    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
    DarrylBurke likes this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. set gender and selected combobox value
    By wynects in forum Java Servlet
    Replies: 0
    Last Post: 12-21-2010, 06:03 AM

Posting Permissions

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