gpt4 book ai didi

java - 如果两个复选框都被选中怎么办?

转载 作者:行者123 更新时间:2023-11-29 03:04:18 25 4
gpt4 key购买 nike

在这个程序中,当两个复选框都被选中时,我无法满足第三个 else if 条件。它仍然显示 40% 的价格,而它应该显示 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);

最佳答案

此代码包含一个简单的逻辑错误:

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

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

如果最后一个条件匹配,则前两个条件已经满足,程序将执行第一个条件中的代码。检查两个复选框是否都被选中必须是第一个条件。否则,除非不满足,否则代码永远不会达到该条件。

通过简单地删除此检查 == true 可以大大简化代码,它也只是一个 boolean 值。只需将 if(someBooleanExpression == true) 替换为 if(someBooleanExpression)

关于java - 如果两个复选框都被选中怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32906202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com