Java如果Statement没有找到正确的语句

时间:2013-04-10 01:52:35

标签: java if-statement

这是一个学校课程,您输入银行的支票数量,并计算当月的费用。当我把超过20个支票的金额用于使用错误的其他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;
    }
    }

DialogBox类:

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());
}
}

5 个答案:

答案 0 :(得分:1)

根据这个条件,如果amountOfChecks为20,则满足以下条件,并且将输入此条件。

else if (amountOfChecks >=11)

将其更改为

else if (amountOfChecks >= 11 && amountOfChecks < 21)

答案 1 :(得分:0)

if条件按顺序执行。这导致错误。

public String 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;
}

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

答案 2 :(得分:0)

你写道:

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

这里的第一个条件总是包含第二个,所以你永远不会到达第二个。也就是说,只要大于21,支票的数量就会大于11,所以你应该从if(amount>1001)开始检查最大到最小的订单。

答案 3 :(得分:0)

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

实施例:     否则if(amountOfChecks&gt; = 11&amp;&amp; amountOfChecks&lt; 21){ }

答案 4 :(得分:0)

花点时间考虑一下你的逻辑。

假设amountOfChecks == 9

  • amountOfChecks <= 10true,因此未检查其他if条款,这意味着它正在跳过== 0< 0

更好的陈述是......

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

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

  • amountOfChecks >= 11true,不再检查if条款。

    否则if(amountOfChecks&gt; 10&amp;&amp; amountOfChecks&lt; 21){...} 否则if(amountOfChecks&gt; 20&amp;&amp; amountOfChecks&lt; 31){...} 否则if(amountOfChecks&gt; 30&amp;&amp; amountOfChecks&lt; 1000){...} 否则{...} //其他所有

您需要在给定的if语句中允许范围。

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