微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我在以下Java代码上遇到错误应该在显示状态的else if语句下使用if语句吗?

如何解决我在以下Java代码上遇到错误应该在显示状态的else if语句下使用if语句吗?

我正在编写一个程序,其中,根据选择的0和3之间的状态以及收入值,将使用if语句计算解决方案。我的错误消息是:

线程“ main”中的异常java.lang.Error:未解决的编译问题:在Assign2.main(Assign2.java:9)

import java.util.Scanner; 
    
    public class Assign2 {
      public static void main(String[] args) {
        // Create a Scanner
        Scanner input = new Scanner(system.in);
    
        // Prompt the user to enter filing status
        System.out.print("(0-single filer,1-married jointly or " +
         "qualifying widow(er),2-married separately,3-head of " +
         "household) Enter the filing status: ");
       
       int status = input.nextInt();
   
       // Prompt the user to enter taxable income
       System.out.print("Enter the taxable income: ");
       double income = input.nextDouble();
   
       // Compute tax
       double tax = 0;
   
       if (status == 0) { // Compute tax for single filers
         if (income <= 8350)
           tax = income * 0.10;
         else if (income <= 33950)
           tax = 8350 * 0.10 + (income - 8350) * 0.15;
         else if (income <= 82250)
           tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
             (income - 33950) * 0.25;
         else if (income <= 171550)
           tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
             (82250 - 33950) * 0.25 + (income - 82250) * 0.28;
         else if (income <= 372950)
           tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
             (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
             (income - 171550) * 0.33;
         else
           tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
             (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
             (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
       }
       else if (status == 1) { // Compute tax for married file jointly
         if (income <= 16700)
             tax = income * 0.10;
         else if (income <= 67900)
             tax = 16700 * 0.10 + (income - 16700) * 0.15;
         else if (income <= 137050)
             tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25;
         else if (income <= 208850)
             tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +
               (137050 - 67900) * 0.25 + (income - 137050) * 0.28;
         else if (income <= 372950)
             tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +
               (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 +
               (income - 208850) * 0.33;
         else
             tax = 16700 * 0.10 + (67900 - 16700) * 0.15 +
             (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 +
             (372950 - 208850) * 0.33 + (income - 372950) * 0.35;
             
       }
       else if (status == 2) { // Compute tax for married separately
         // Left as exercise
           if (income <= 8350)
             tax = income * 0.10;
           else if (income <= 33950)
             tax = 8350 * 0.10 + (income - 8350) * 0.15;
           else if (income <= 68525)
             tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
           else if (income <= 104425)
                 tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;
           else if (income <= 186475)
             tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + 
             (income - 104425) * 0.33;
           else  
                 tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + 
                 (income - 104425) * 0.33 + (income - 186475) * 0.35; 
       }
       else if (status == 3) { // Compute tax for head of household
         // Left as exercise
           if (income <= 11950)
                 tax = income * 0.10;
           else if (income <= 45500)
                 tax = 11950 * 0.10 + (income - 11950) * 0.15;
           else if (income <= 117450)
                 tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
           else if (income <= 190200)
             tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;
           else if (income <= 372950)
                 tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + 
                 (income - 190200) * 0.33;
           else  
             tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + 
             (income - 190200) * 0.33 + (income - 372950) * 0.35; 
       }
       else {
         System.out.println("Error: invalid status");
         System.exit(1);
       }
   
       // display the result
       System.out.println("Tax is " + (int)(tax * 100) / 100.0);
         
      
     }
   }
 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。