简体   繁体   中英

What if both the Checkboxes are selected?

In this program i'm not able to fulfill the 3rd else if condition that is when both the check-boxes are selected.It is still displaying 40% of the price whereas it should display 50%.

int p      = Integer.parseInt(jTextField1.getText()); //Product Price
int dis    = 40*p/100; // 40% discount
int adis   = 10*p/100; // 10% discount
int tdis   = 50*p/100; // 50% discount
int fpd    = p-dis;    // final price (40% discount)
int fpad   = p-adis;   // final price (10% discount)
int fptdis = p-tdis;   // final price (50% discount)

if (jCheckBox1.isSelected()==true)
    jLabel3.setText("dis is 40% "+"dis amt is "+dis+"final price is "+fpd);
else if (jCheckBox2.isSelected()==true)
    jLabel3.setText("dis is 10% "+"dis amt is "+adis+"final price is "+fpad);
// This condition is not working!!
else if (jCheckBox1.isSelected()==true && jCheckBox2.isSelected()==true)
    jLabel3.setText("dis is 50% "+"dis amt is "+tdis+"final price is "+fptdis);
else jLabel3.setText("no discount"+"final price is "+p);

This code contains a simple logic fault:

if (jCheckBox1.isSelected()==true)
    else if (jCheckBox2.isSelected()==true)

// This condition is not working!!
    else if (jCheckBox1.isSelected()==true && jCheckBox2.isSelected()==true)

If the last condition would match, the first two are already fulfilled and the program executes the code in the first condition. Checking if both checkboxes are selected must be the first condition. Otherwise the code will never reach that condition unless it's not satisfied.

And the code can be simplified a lot by simply removing this check == true , which is aswell just a boolean value. Simply replace if(someBooleanExpression == true) with if(someBooleanExpression) .

You should reorder your if else's. First check for both and then for individual. Because if any one of them is checked, the check stops at first two conditions itself.

    if (jCheckBox1.isSelected() && jCheckBox2.isSelected())
        jLabel3.setText("dis is 50% "+"dis amt is "+tdis+"final price is "+fptdis);
    else if (jCheckBox1.isSelected())
        jLabel3.setText("dis is 40% "+"dis amt is "+dis+"final price is "+fpd);
    else if (jCheckBox2.isSelected())
        jLabel3.setText("dis is 10% "+"dis amt is "+adis+"final price is "+fpad);

    else jLabel3.setText("no discount"+"final price is "+p);

Moreover I replaced the part jCheckBox1.isSelected() == true with jCheckBox1.isSelected() because it is already returning a boolean you need not to compare again with boolean.

boolean ch1 = jCheckBox1.isSelected();
boolean ch2 = jCheckBox1.isSelected();

if (ch1 && !ch2) {
    // first case
} else if (!ch1 && ch2) {
    // second case
} else if (ch1 && ch2) {
    // third case
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM