0
if (annual <= lowincome && aChar == 'S')
    owe = annual * .25;
else if ( annual > lowincome || annual < medincome && aChar == 'S')
    owe = annual * .50;
else if (annual > medincome && Char == 'S')
    owe = annual * .75;
else if (aChar == 'M' && annual <= lowincome)
    owe = annual *.24;
else if(annual > low income || annual < medincome && aChar == 'M')
    owe = annual * .49;
else if(annual > medincome && Char == 'M')
    owe = annual * .74;

我希望用户输入他们是已婚还是单身,然后在找出年收入后进行计算。我收到此错误

TaxReturn.java:54: error: ')' expect
if(annual > low income || annual < medincome && aChar == 'M')
                                           ^
TaxReturn.java:54: error: not a statement
if(annual > low income || annual < medincome && aChar == 'M')
                                                   ^
TaxReturn.java:54: error: ';' expect
if(annual > low income || annual < medincome && aChar == 'M')

TaxReturn.java:56: error: 'else' without 'if'
else 
                            ^
TaxReturn.java:68: error: class, interface, or enum expected
}       
^
5 errors
4

5 回答 5

6
if(annual > low income || annual < medincome && aChar == 'M')

应该

if(annual > lowincome || annual < medincome && aChar == 'M')

既不low也不income被识别为声明的变量。

于 2013-01-07T14:50:07.650 回答
1

You should use brackets for give perfect meaning on your conditions...

if ( annual  >  low income  ||(  annual  <  medincome  &&  aChar  == 'M' )) ... 
于 2013-01-07T14:53:34.460 回答
1

这来自您的错误跟踪

if(annual > low income || annual < medincome && aChar == 'M')

你不能在变量内有空间,就像在你的low income

于 2013-01-07T16:25:55.200 回答
1

您应该将此作为返回欠款金额的辅助方法,这将使您摆脱讨厌的嵌套业务。如果嵌套了三个以上的 if 语句,则需要重新考虑如何解决问题。我向你保证,你会因此而失分。

话虽如此,由于“低”和“收入”之间的空间,您的代码正在爆炸......应该是“低收入”。

这是您可能考虑如何重新组织代码的示例...

public BigDecimal getOwed(BigDecimal annual){
    if (annual <= lowincome && aChar == 'S'){
        return owe = annual * .25;
    }

    if ( annual > lowincome || annual < medincome && aChar == 'S'){
           return owe = annual * .50;
    }

    if (annual > medincome && Char == 'S'){
        return owe = annual * .75;
    }
    /*go through all your cases in this fashion
    .
    .
    .
    handle the case that it doesnt match any (should never occur).
    */

}

这种方式更干净,更容易阅读。

于 2013-01-07T15:15:40.610 回答
1

这段代码不应该:

if (annual > medincome && Char == 'S'){
    return owe = annual * .75;
}

是这样的:

if (annual > medincome && aChar == 'S'){
    return owe = annual * .75;
}

看起来您没有始终如一地评估您的变量 aChar 。您也可以在最后一个“else if”语句中执行此操作。

于 2013-01-07T16:38:34.937 回答