开发者

Nested if else not working

My nested if else is not working.

Here is the code:

if(s.getSelectedItem().toString().equals("Margherita")){
    if(s1.getSelectedItem().toString().equals("Personal")){
        rowbill=65*n;
    }
            if(s1.getSelectedI开发者_Python百科tem().toString().equals("Medium")){
        rowbill=125*n;
    }
            if(s1.getSelectedItem().toString().equals("Family")){
        rowbill=240*n;
    }

}else if(s.getSelectedItem().toString().equals("Simply Veg")){
    if(s1.getSelectedItem().toString().equals("Personal")){
        rowbill=75*n;
    }
    if(s1.getSelectedItem().toString().equals("Medium")){
        rowbill=145*n;
    }
    if(s1.getSelectedItem().toString().equals("Family")){
        rowbill=285*n;
    }
}else if(s.getSelectedItem().toString().equals("Veggie Crunch")){
    if(s1.getSelectedItem().toString().equals("Personal")){
        rowbill=100*n;
    }
    if(s1.getSelectedItem().toString().equals("Medium")){
        rowbill=195*n;
    }
    if(s1.getSelectedItem().toString().equals("Family")){
        rowbill=350*n;
    }
}   


As you probably have not discovered a failure with the compiler, the trouble is due to your structure.

Simplify it by removing the else's. Yes, you'll end doing up to two String equals() more each time. You can affor it.

Write it as

System.out.println("Marguerite");
if (s.getSelectedItem().toString().equals("Marguerite")) {
    System.out.println("Marguerite");
    ....
}
if (s.getSelectedItem().toString().equals("Simply Veg")) {
    System.out.println("Simply Veg");
    ....
}

The error will be then easier to spot. Once solved, you can go back to nested if-else-if if you want to.


i think missing or extra '{','}' somewhere.

a logic bug maybe, 'cause compiler doesn't show error or warning.


It may work...

`if(s.getSelectedItem().toString().trim().equals("Margherita")){
if(s1.getSelectedItem().toString().trim().equals("Personal")){
    rowbill=65*n;
}
else if(s1.getSelectedItem().toString().trim().equals("Medium")){
    rowbill=125*n;
}
else if(s1.getSelectedItem().toString().trim().equals("Family")){
    rowbill=240*n;
}

}else if(s.getSelectedItem().toString().trim().equals("Simply Veg")){
if(s1.getSelectedItem().toString().trim().equals("Personal")){
    rowbill=75*n;
}
else if(s1.getSelectedItem().toString().trim().equals("Medium")){
    rowbill=145*n;
}
else if(s1.getSelectedItem().toString().trim().equals("Family")){
    rowbill=285*n;
}
}else if(s.getSelectedItem().toString().trim().equals("Veggie Crunch")){
if(s1.getSelectedItem().toString().trim().equals("Personal")){
    rowbill=100*n;
}
else if(s1.getSelectedItem().toString().trim().equals("Medium")){
    rowbill=195*n;
}
else if(s1.getSelectedItem().toString().trim().equals("Family")){
    rowbill=350*n;
}
}`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜