2012-02-24 76 views
-2

編輯的問題被縮小:打印輸出/ s的嵌套if/else語句

ans1 = homeMortgage + annualTuition + annualCharity + healthCredit; 
    if(grossAnnualIncome <= ans1) 
    { 
    System.out.println("YOU OWE $0.00 IN TAXES!"); 
    } 
    else if(grossAnnualIncome == 0) 
    { 
    System.out.println("You earned no income so you owe no taxes!"); 
    } 
    else 
    { 
    //GROSS ANNUAL INCOME > 0 OUTPUT 
    System.out.printf("\nYOUR TAXES\n\n" 
        + "Gross Annual Income: %,.0f\n\n" 
        + "Deductions: \n" 
        + "\tHigher Education: %,.0f\n" 
        + "\tCharitable Contributions: %,.0f\n" 
        + "\tHome Mortgage Interest: %,.0f\n" 
        + "\tHealth Insurance Tax Credit: %,.0f\n\n" 
        + "Tax at 17%%: %,.0f\n" 
        + "Annual Income After Taxes: %,.0f\n" 
        + "Monthly Income After Taxes: %,.0f", grossAnnualIncome, annualTuition, annualCharity, 
         homeMortgage, healthCredit, taxAt17, annualAfterTaxes, monthlyAfterTaxes); 
    } 

grossAnnualIncome < = ANS1正確輸出。

grossAnnualIncome == 0輸出您OWE $ 0.00的所有稅金!而不是你沒有收入,所以你不欠稅!

grossAnnualIncome> 0輸出正確。

+1

也許你可以使用「其他如果」? – Andrew 2012-02-24 17:27:08

+0

如果我輸入0作爲總的年收入,該方案只打印:「你沒有賺得收入,所以你欠稅!」這似乎是你期望的 - 不知道我明白你想要什麼...... – assylias 2012-02-24 17:28:24

+0

需要有對於有收入的用戶的輸出,但是他們的稅收抵免超過了他們的納稅義務,因此輸出讀數您只需$ 0.00納稅!我可以把它展示出來,但是它會隨着它一起發佈大部分每年都會收到的東西,我不想要。 – Abweichung 2012-02-24 17:29:52

回答

1

編輯...

這其實很簡單的發生在這裏。您注意到第一個塊中的語句已打印。這意味着第一個測試條件爲真 - 即grossAnnualIncome(零)小於ans1(在這種情況下也爲零)。

我懷疑解決這個問題的方法是切換前兩個測試的順序,以便零收入檢查優先。

在一般情況下,如果有可能重疊的情況,請確保您爲了在if - else if - else ...塊的試驗方法,這樣最具體的一個先出現。


(順便說一下,如果你只是輸出一個簡單的字符串它更平常打電話System.out.print(或println),而不是printf的,沒有一點使得Java格式沒什麼畢竟,我發現它稍迷失方向)。

+0

好的,這固定了我的第三個輸出語句,但我在下面的第二個語句中遇到問題。讓我重新編輯我的帖子。 – Abweichung 2012-02-24 17:53:14

+0

就是這樣。正確地注意if - else if - else重疊,並首先從最具體的條件開始。現在這樣做是有道理的,但我不知道如果你不這樣做會導致問題。 再次謝謝你! – Abweichung 2012-02-24 18:13:48

0

如果我理解正確的話,你可以只移動最後的輸出了if(grossAnnualIncome > 0)語句來的,稍後打印:根據新的問題

if(grossAnnualIncome > 0) 
    { 

    //... You do a whole lot of stuff here that i removed in my example 
    // to make it easier to read. 

    taxableIncome = grossAnnualIncome - homeMortgage - annualTuition - annualCharity - healthCredit; 
     taxAt17 = taxableIncome * .17; 
     annualAfterTaxes = grossAnnualIncome - taxAt17; 
     monthlyAfterTaxes = annualAfterTaxes/12; 
     ans1 = homeMortgage + annualTuition + annualCharity + healthCredit; 


    // Here I removed the output and moved it to the else statement below, to only 
    // print it if income is greater than 0 

} 

    if(grossAnnualIncome <= 0) 
    { 
     System.out.printf("You earned no income so you owe no taxes!"); 
    } else { 
     System.out.printf("\nYOUR TAXES\n\n" 
        + "Gross Annual Income: %,.0f\n\n" 
        + "Deductions: \n" 
        + "\tHigher Education: %,.0f\n" 
        + "\tCharitable Contributions: %,.0f\n" 
        + "\tHome Mortgage Interest: %,.0f\n" 
        + "\tHealth Insurance Tax Credit: %,.0f\n\n" 
        + "Tax at 17%%: %,.0f\n" 
        + "Annual Income After Taxes: %,.0f\n" 
        + "Monthly Income After Taxes: %,.0f", grossAnnualIncome, annualTuition, annualCharity, 
         homeMortgage, healthCredit, taxAt17, annualAfterTaxes, monthlyAfterTaxes); 
}