0

这是一个学校计划,您可以输入银行的支票数量并计算当月的费用。当我放入超过 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());
}
}
4

5 回答 5

1

基于此条件,如果amountOfChecks 为20,则满足以下条件,将进入此if 条件。

else if (amountOfChecks >=11)

将其更改为

else if (amountOfChecks >= 11 && amountOfChecks < 21)
于 2013-04-10T01:57:47.773 回答
0

if 条件按顺序执行。那是导致错误的原因。

公共字符串 getTotalPrice() { DecimalFormat df = new DecimalFormat("#,##0.00");

if (amountOfChecks == 0 )
{
    message = "Total is $0.50";
}

else if (amountOfChecks <0)
{
    message = "Invalid amount of checks... Total is $0.50";
}


else if (amountOfChecks <= 10)
{
    total = (amountOfChecks * .14) + .50;
    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;
}

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 >=11)
{
    amountOver10 = amountOfChecks - 10;
    total = (10*.14) + (amountOver10*.13) + .50;
    formatTotal = df.format(total);
    message = "Your total is: $" + formatTotal;
}
return message;
}

在你的情况下,你也会有 case amountOfChecks <0 的错误。这是因为如果amountOfChecks 是-5,那么在检查它不等于0 但小于10 之后,它会进入第二个条件,即amountofChecks <= 10,并且不会进入所需的条件。希望有帮助。

于 2013-04-10T01:57:49.897 回答
0

你写:

if(...)
else if (amountOfChecks >=11)//always stops here
{
    ...
}
else if (amountOfChecks >= 21)//never reached.
{
    ...
}

这里的第一个条件总是包括第二个,所以你永远不会到达第二个。也就是说,只要大于 21,检查的次数就大于 11,所以你应该按照从大到小的顺序检查,从 开始if(amount>1001)

于 2013-04-10T01:59:01.357 回答
0

当前代码的排序方式是,代码在检查 21 之前检查 amountOfChecks 是否大于/等于 11。假设您输入了 25 个检查。25 >= 11,它也是 >= 21。因为您的程序首先检查 >= 11,所以永远不会达到 else if(amountOfChecks >= 21)。切换顺序以便首先检查较大的数字,或者更改顺序以便检查范围。

示例:else if(amountOfChecks >= 11 && amountOfChecks<21){ }

于 2013-04-10T01:59:56.853 回答
0

花点时间想想你的逻辑。

假设amountOfChecks == 9.

  • amountOfChecks <= 10is true,因此没有if检查其他语句,这意味着它正在跳过== 0并且< 0

更好的说法是...

if (amountOfChecks > 0 && amountOfChecks <= 10) {...}
else if (amountOfChecks == 0) {...}
else if (amountOfCheck < 0) {...}

现在,如果amountOfChecks == 11...

  • amountOfChecks >= 11istrue并且不再if检查任何语句。

    else if (amountOfChecks > 10 && amountOfChecks < 21) {...} else if (amountOfChecks > 20 && amountOfChecks < 31) {...} else if (amountOfChecks > 30 && amountOfChecks < 1000) {...} else { ...} // 其他所有内容

if您需要在给定语句本身内允许范围。

(我不敢相信我要说这个)办公桌用一张纸和笔检查你的逻辑会非常有用......

于 2013-04-10T02:03:54.003 回答