2013-01-07 57 views
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; 

我希望用戶輸入,如果他們是已婚還是單身然後去雖然計算髮現那裏每年income.I後得到這個錯誤我得到一個錯誤的最後3如果else語句

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 
+0

[標籤:家庭作業]? **這個標籤是OBSOLETE **。 –

+1

爲什麼你不喜歡大括號?無論如何,這是你的完整代碼嗎? –

+0

OMG太多if-else-if-else .. !!你的代碼中存在錯誤,我建議你逐個剖析,你將會知道你在哪裏做過錯誤:) –

回答

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

應該

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

無論low也不income被認定爲十進制拉雷變量。

1

您應該使用括號給你的條件完全意義...

if (annual > low income ||( annual < medincome && aChar == 'M')) ... 
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). 
    */ 

} 

這種方式是更清潔和更容易閱讀。

1

這個來自您的錯誤跟蹤

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

你不能有空間內可變的,就像你low income

1

不應把該代碼:

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

是這樣的:

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

它看起來像你不一致評估你的變量aChar。你也可以在最後的「else if」語句中執行此操作。