在嵌套的if / else语句上打印输出

时间:2012-02-24 17:22:45

标签: java if-statement printf java.util.scanner

随着问题的缩小而进行编辑:

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&lt; = ans1正确输出。

grossAnnualIncome == 0输出你欠税的0.00美元!而不是你没有收入,所以你欠税!

grossAnnualIncome&gt; 0输出正确。

2 个答案:

答案 0 :(得分:1)

根据新问题编辑...

实际上这里发生的事情非常简单。您正在观察第一个块中的语句是否已打印。这意味着第一个测试条件为真 - 即grossAnnualIncome(零)小于ans1(在这种情况下也为零)。

我怀疑修复此问题的方法是切换前两个测试的顺序,以便优先检查零收入。

一般情况下,如果条件可能重叠,请确保在if - else if - else ...块中对测试进行排序,以便首先显示最具体的测试。


(顺便说一下,如果你只是输出一个简单的字符串,那么调用System.out.print(或println)而不是printf更常见。毕竟,没有任何意义可以使Java格式化,我发现它有些迷惑。)

答案 1 :(得分: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);
}