我是一个初学者,所以这个结果我得到了我的if else声明的一个条件让我大吃一惊。一切正常,除了QtyCalc变量> = 100时的情况.Finprice变量被列为光盘变量,我无法弄清楚原因。救命?
import javax.swing.JOptionPane;
public class SoftwareSales {
public static void main(String[] args) {
final int price = 99;
String Qty;
double QtyCalc, preprice, Finprice, disc;
Qty = JOptionPane.showInputDialog(null, "How many packages will you buy?");
QtyCalc = Double.parseDouble(Qty);
preprice = QtyCalc * price;
if (QtyCalc >= 100) {
disc = (preprice * (0.5));
Finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, "Your discount is: " + disc + ".\n" + "Your final price is: " + Finprice + ". ");
} else if (QtyCalc >= 50 && QtyCalc <= 99) {
disc = (preprice * 0.4);
Finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, "Your discount is: " + disc + ".\n" + "Your final price is: " + Finprice + ". ");
} else if (QtyCalc >= 20 && QtyCalc <= 49) {
disc = (preprice * 0.3);
Finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, "Your discount is: " + disc + ".\n" + "Your final price is: " + Finprice + ". ");
} else if (QtyCalc >= 10 && QtyCalc <= 19) {
disc = (preprice * 0.2);
Finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, "Your discount is: " + disc + ".\n" + "Your final price is: " + Finprice + ". ");
} else if (QtyCalc < 10 && QtyCalc >= 1) {
disc = 0;
Finprice = 0;
JOptionPane.showMessageDialog(null, "Sorry, there is no discount for purchases less than 10." + " Your price is: " + preprice);
} else {
JOptionPane.showMessageDialog(null, "You have entered an invalid number.");
}
disc = 0;
Finprice = 0;
System.exit(0);
}
}
答案 0 :(得分:1)
当数量> = 100时,最终价格等于折扣,因为折扣是50%...查看您发布的代码。特别是:
if (QtyCalc >= 100) {
disc = (preprice * (0.5));
Finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, "Your discount is: " + disc + ".\n" + "Your final price is: " + Finprice + ". ");
}
答案 1 :(得分:0)
没有奇怪的行为,只是简单的数学:
disc = (preprice * (0.5));
Finprice = (preprice - disc);
您还应考虑删除冗余代码:
import javax.swing.JOptionPane;
public class SoftwareSales {
public static void main(String[] args) {
final int PRICE = 99;
String qty;
double qtyCalc, preprice, finprice, disc;
disc = 0;
qtyCalc = 0;
boolean invalid = false;
do{
invalid = false;
qty = JOptionPane.showInputDialog(null, "How many packages will you buy?");
if (qty == null){
return;
}
try{
qtyCalc = Double.parseDouble(qty);
}catch(NumberFormatException e){
invalid = true;
}
}while(invalid);
preprice = qtyCalc * PRICE;
if (qtyCalc >= 100) {
disc = (preprice * (0.5));
} else if (qtyCalc >= 50 && qtyCalc <= 99) {
disc = (preprice * 0.4);
} else if (qtyCalc >= 20 && qtyCalc <= 49) {
disc = (preprice * 0.3);
} else if (qtyCalc >= 10 && qtyCalc <= 19) {
disc = (preprice * 0.2);
} else if (qtyCalc < 10 && qtyCalc >= 1) {
disc = 0;
finprice = 0;
JOptionPane.showMessageDialog(null, "Sorry, there is no discount for purchases less than 10. Your price is: " + preprice);
return;
} else {
JOptionPane.showMessageDialog(null, "You have entered an invalid number.");
return;
}
finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, String.format("Your discount is: %.2f.\nYour final price is: %.2f. ",disc,finprice));
}
}
还要记住变量的java约定:总是以小写字母开头。 总决赛IN UPPER-CASE