Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: using if/else statments

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation using if/else statments

    my program is supposed to show "correct" when color box goes into "colors" box, dark box goes into "darks" box and white box goes into "whites" box. it is also supposed to create a new color if the user is correct. What am I doing wrong?

    if(colorsum <600 && colorsum>230 && laundrydark.contains(point) )

    correct++;
    swatch.setFillColor(randomcolor);


    if ((colorsum < 230) && laundrycolor.contains(point) )

    correct++;
    swatch.setFillColor(randomcolor);

    if((colorsum > 600) && laundrywhite.contains(point))

    correct++;
    swatch.setFillColor(randomcolor);

    if( colorsum <=600 && colorsum >=230 && laundrywhite.contains(point) ||
    colorsum <=230 && laundrydark.contains(point) ||
    colorsum >=600 && laundrycolor.contains(point))

    incorrect ++;
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: using if/else statments

    I have a feeling it's because you're forgetting to bracket each if statement. I'd also recommend doing if/else statements, since only 1 of these can ever be executed each time through. By re-arranging your tests, you can also remove a lot of checks.

    if ((colorsum < 230) && laundrycolor.contains(point) )
    {
    correct++;
    swatch.setFillColor(randomcolor);
    }
    else if(colorsum <600 && laundrydark.contains(point) )
    {
    correct++;
    swatch.setFillColor(randomcolor);
    }
    else if(laundrywhite.contains(point))
    {
    correct++;
    swatch.setFillColor(randomcolor);
    }
    else
    {
    incorrect ++;
    }