Friday, 15 January 2010

Java if statement not working as expected -



Java if statement not working as expected -

i'm studying java programming exam, , i'm still beginner. problem in if statement is:

int z; if (z==1); {//calculates area} if (z==2) {//calculates volume}

the goal if user chooses 1 he'll find area calculated , if user chooses 2 volume calculated. however, in output area , volume beingness calculated whatever user chooses. why that?

you have remove ; after condition. otherwise, if statement empty, , code block next executed.

if (z==1) {//calculates area} if (z==2) {//calculates volume}

or better:

if (z==1) { //calculates area } else if (z==2) { //calculates volume }

since both conditions can't true.

java if-statement

No comments:

Post a Comment