我收到了最后3条if else语句的错误

时间:2013-01-07 14:46:50

标签: java helper

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

5 个答案:

答案 0 :(得分:6)

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

应该是

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

lowincome都不会被识别为声明的变量。

答案 1 :(得分:1)

你应该使用括号在你的条件下给出完美的意义......

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

答案 2 :(得分: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).
    */

}

这种方式更清晰,更易于阅读。

答案 3 :(得分:1)

这来自您的错误跟踪

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

你不能在变量中包含空格,就像你的low income

一样

答案 4 :(得分:1)

不应该这段代码:

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

是这样的:

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

看起来您并没有始终如一地评估变量aChar。你也可以在最后的“else if”语句中这样做。