作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在这个程序中,当两个复选框都被选中时,我无法满足第三个 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/
我是一名优秀的程序员,十分优秀!