这是一个学校计划,您可以输入银行的支票数量并计算当月的费用。当我放入超过 20 张支票的金额时,它使用了错误的 else if 语句!
主类:
import java.text.DecimalFormat;
public class WeSavU
{
private int amountOfChecks, amountOver10, amountOver20, amountOver30;
private double total;
private String message, formatTotal;
public WeSavU(int checks)
{
amountOfChecks = checks;
}
public void setNumChecks(int checks)
{
amountOfChecks = checks;
}
public String getTotalPrice()
{
DecimalFormat df = new DecimalFormat("#,##0.00");
if (amountOfChecks <= 10)
{
total = (amountOfChecks * .14) + .50;
formatTotal = df.format(total);
message = "Your total is: $" + formatTotal;
}
else if (amountOfChecks == 0 )
{
message = "Total is $0.50";
}
else if (amountOfChecks <0)
{
message = "Invalid amount of checks... Total is $0.50";
}
else if (amountOfChecks >=11)
{
amountOver10 = amountOfChecks - 10;
total = (10*.14) + (amountOver10*.13) + .50;
formatTotal = df.format(total);
message = "Your total is: $" + formatTotal;
}
else if (amountOfChecks >= 21)
{
amountOver20 = amountOfChecks - 20;
total = 2.7 + (amountOver20* .12) + .42;
formatTotal = df.format(total);
message = "Your total is: $" + formatTotal;
}
else if (amountOfChecks > 1000)
{
message = "You need a corporate account!";
}
else if (amountOfChecks >= 31 && amountOfChecks < 1001)
{
amountOver30 = amountOfChecks - 30;
total = 3.9 + (amountOver30*.11) + .50;
formatTotal = df.format(total);
message = "Your total is: $" + formatTotal;
}
return message;
}
}
对话框类:
import javax.swing.JOptionPane;
public class WeSavUDialog
{
public static void main(String[] args)
{
String checks;
int Check;
checks = JOptionPane.showInputDialog("Enter the amount of checks:");
Check = Integer.parseInt(checks);
WeSavU check = new WeSavU(Check);
JOptionPane.showMessageDialog(null, check.getTotalPrice());
}
}