我正在寻找一个原因,无论我为discountPercentage输入消息框输入什么,此代码将仅使用下面列出的10%折扣方案。我需要做到这一点,以便计算的唯一百分比是10%,15%或25%。如果用户在discountPercentage输入消息框中输入任何其他内容,折扣将默认为价格1的10%折扣。
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Assignment3 {
public static void main(String[] args) {
// Calculate the discount price of an item
// Convert price to display as decimal format
DecimalFormat df = new
DecimalFormat("#,###,##0.00");
// Declare discountPercentage variable
// Display message boxes for price and percentage entries
double price1 = Double.parseDouble(JOptionPane.showInputDialog( null, " Enter item price (ex:2.35): "));
double discountPercentage = Double.parseDouble(JOptionPane.showInputDialog( null, " Enter discount percentage "));
int dicountPercentage = 0;
// Declare if else statements for output calculations
if (discountPercentage > 25){
JOptionPane.showMessageDialog(null, " The discounted price is: " + "$" + df.format(price1 - .10 * price1));
}else if (dicountPercentage == 25){
JOptionPane.showMessageDialog(null, " The discounted price is: " + "$" + df.format(price1 - .25 * price1));
} else if (discountPercentage > 15){
JOptionPane.showMessageDialog(null, " The discounted price is: " + "$" + df.format(price1 - .10 * price1));
} else if (dicountPercentage == 15){
JOptionPane.showMessageDialog(null, " The discounted price is: " + "$" + df.format(price1 - .15 * price1));
} else if (discountPercentage >= 10){
JOptionPane.showMessageDialog(null, " The discounted price is: " + "$" + df.format(price1 - .10 * price1));
}
}
由于
答案 0 :(得分:7)
您的代码每次都会使用10%的情况,因为在discountPercentage
的所有条件中,您使用值.10
,每次都是完全相同的行:
JOptionPane.showMessageDialog(null, " The discounted price is: " + "$" + df.format(price1 - .10 * price1));
也就是说,在所有这些条件下,你都有同样的路线:
if (discountPercentage > 25){ } else if (discountPercentage > 15){ } else if (discountPercentage >= 10){
另请注意,此代码中有两个变量名称几乎相同:
dicountPercentage
discountPercentage
并且第一个变量设置为0且永远不会改变,
所以使用dicountPercentage
的所有条件都毫无意义。
这种命名非常糟糕:
避免使用名称非常相似的变量。
(起初我甚至没有注意到有两个变量,
我以为两者都是一样的......)
答案 1 :(得分:0)
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Assignment3 {
public static void main(String[] args) {
// Calculate the discount price of an item
// Convert price to display as decimal format
DecimalFormat df = new
DecimalFormat("#,###,##0.00");
// Declare discountPercentage variable
// Display message boxes for price and percentage entries
double price1 = Double.parseDouble(JOptionPane.showInputDialog( null, " Enter item price (ex:2.35): "));
double discountPercentage = Double.parseDouble(JOptionPane.showInputDialog( null, " Enter discount percentage "));
//int dicountPercentage = 0;
// Declare if else statements for output calculations
switch(discountPercentage)
{
case 25:
JOptionPane.showMessageDialog(null, " The discounted price is: " + "$" + df.format(price1 - .25 *price1));
break;
case 15:
JOptionPane.showMessageDialog(null, " The discounted price is: " + "$" + df.format(price1 - .15 *price1));
break;
default:
JOptionPane.showMessageDialog(null, " The discounted price is: " + "$" + df.format(price1 - .10 *price1));
break;
}
}