2012-02-24 88 views
0

对不起,再次编辑这个,而不是另一个帖子,我想我会问这里。嵌套如果/其他陈述

public static void main(String[] args) 
{ 
    // TODO code application logic here 

char response = 0; 
double grossAnnualIncome = 0.0; 
double annualTuition = 0.0; 
double annualCharity = 0.0; 
double homeMortgage = 0.0; 
double healthCredit = 0.0; 
double annualAfterTaxes = 0.0; 
double monthlyAfterTaxes = 0.0; 
double taxAt17 = 0.0; 
double taxableIncome = 0.0; 
double ans1 = 0.0; 

Scanner input = new Scanner(System.in); 

System.out.printf("Please enter your gross annual income or 0 for none: "); 
grossAnnualIncome = input.nextDouble(); 

if(grossAnnualIncome > 0) 
    { 
    System.out.printf("Please enter your annual tuition and expenses for higher education or 0 for none: "); 
    annualTuition = input.nextDouble(); 

System.out.printf("Please enter your annual charitable contributions or 0 for none: "); 
    annualCharity = input.nextDouble(); 

    System.out.printf("Please enter the annual interest paid for your home mortgage or 0 for none: "); 
    homeMortgage = input.nextDouble(); 
    input.nextLine(); 

    System.out.printf("Did you purchase health insurance through your employer or outside the workplace?" 
       + " Enter 'Y' or 'N': "); 
response = input.nextLine().charAt(0); 


     if(Character.toUpperCase(response) == 'Y') 
     { 
     System.out.printf("Are you filing as a family? Enter 'Y' or 'N': "); 
     response = input.nextLine().charAt(0); 

      if (Character.toUpperCase(response) == 'Y') 
      { 
      healthCredit = 3500; 
      } 
     else 
     { 

     if(Character.toUpperCase(response) == 'N') 
      { 
       System.out.printf("Are you filing as single? Enter 'Y' or 'N': "); 
       response = input.nextLine().charAt(0); 
      } 
        if(Character.toUpperCase(response) == 'Y') 
        { 
        healthCredit = 2000; 
        } 

     } 

     } 
     else 
     { 
     healthCredit = 0; 
     } 

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

    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); 


} 

    if(grossAnnualIncome <= 0) 
    { 
    System.out.printf("You earned no income so you owe no taxes!"); 
    } 

    System.exit(0); 
    } 

} 

我的两个输出都是正确的,但是我对最后一个输出有问题。

我最后的输出结果需要阅读“您只需要0.00美元的税金!”

当我代码:

if (grossAnnualIncome <= ans1) 
{ 
System.out.printf("YOU OWE $0.00 IN TAXES!"); 
} 

它显示了从grossAnnualIncome> 0输出。

+0

打印你给的输入......还有你在哪一行得到这个异常? – 2012-02-24 05:02:59

+0

我不确定哪一行,上面的代码片段就是发布的异常错误。 至于我输入: 请输入您的年收入额或0表示无:25000 请输入您每年的学费和费用高等教育或0表示无:2500 请输入您的年度慈善捐款或0表示无:100 请输入您的住房抵押贷款的年利息或0为零:0 您是否通过您的雇主或在工作场所以外购买健康保险?输入'Y'或'N':y 您是否正在申请家庭?输入'Y'或'N':y 然后我收到错误消息。 – Abweichung 2012-02-24 05:07:12

+0

家庭新闻N并告诉我你得到... – 2012-02-24 05:12:41

回答

2

Java: Literal percent sign in printf statement

它可能有17%与百分比做。它需要转义,可以用另一个%

17%% 
+0

我能够修复健康保险代码,我的括号错误。尽管如此,感谢您的帮助!我将用我所拥有的更新我的代码。 – Abweichung 2012-02-24 05:29:31

+0

是的,就是这样。我实际上并不知道我需要另一个,所以应该注意并再次感谢您的帮助! – Abweichung 2012-02-24 05:58:11

+0

我遇到过这个问题,格式化可能很难理解:P – 2012-02-24 07:34:51