【问题标题】:Nesting if statements - cannot get the nested statements to execute嵌套 if 语句 - 无法执行嵌套语句
【发布时间】:2016-03-21 09:55:41
【问题描述】:

我正在尝试使下面的嵌套语句正常工作,但在让它们在第一条语句之后执行时遇到问题。我尝试嵌套语句,但只是第一个 if 执行。非常感谢任何有关格式化的反馈,我知道可能有更有效的方法来实现这一点,但我必须使用嵌套语句执行代码。

package incometax;

import java.util.Scanner;
import java.text.DecimalFormat;

public class IncomeTax {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
DecimalFormat df = new DecimalFormat ("#,###,000.00");

String singleStatus = "single";
String marriedStatus = "married";
String maritalStatus = "";
double annualIncome = 0;
double taxAmount = 0;

System.out.println("Please enter your martial status: ");
maritalStatus = scnr.next();
 if (maritalStatus.compareTo(singleStatus) == 0){
     System.out.println("Please enter your annual income: ");
     annualIncome = scnr.nextDouble();

         if (annualIncome <= 30000){
         taxAmount = (annualIncome * .15);
         System.out.println("Based on annual income of "+ "$ " +        
         df.format(annualIncome) + " your tax is " + "$ " +   
         df.format(taxAmount));

             if (annualIncome > 30000){
             taxAmount = (annualIncome * .25);
             System.out.println("Based on annual income of "+ "$ " +   
             df.format(annualIncome) + 
             " your tax is " + "$ " + df.format(taxAmount));
             }
         }
         else {
              if (maritalStatus.compareTo(marriedStatus) == 0){

                 if(annualIncome <= 30000){
                 taxAmount = (annualIncome * .12);
                 System.out.println("Based on annual income of "+ "$ "  
                 +  df.format(annualIncome) + 
                 " your tax is " + "$ " + df.format(taxAmount));

                     if(annualIncome > 30000){
                     taxAmount = (annualIncome * .20);
                     System.out.println("Based on annual income of "+  
                     "$ " +  df.format(annualIncome) + 
                     " your tax is " + "$ " + df.format(taxAmount));
                     }
                  }
               }
             }
        }  
}
}

【问题讨论】:

  • 你试过调试了吗?
  • 如果你的缩进是一致的,就会更容易看到发生了什么。
  • 你的条件永远不会是真的。例如,在if (annualIncome &lt;= 30000) 的块内,您有if (annualIncome &gt; 30000),这将始终为假,因为annualIncome 没有改变。
  • 不要使用嵌套语句,将它们更改为更简单的语句,使用提前返回模式。
  • 您的 IDE 应该能够为您修复缩进。在我的 Eclipse(在 Mac 上)中,command-shift-F 可以解决问题。

标签: java if-statement nested


【解决方案1】:

annualIncome &lt;= 30000 时,您将进入第一个if。但是嵌套 if 中的条件是annualIncome &gt; 30000。这将永远是错误的。尝试if else 而不是嵌套的if

if (annualIncome <= 30000) {
    taxAmount = (annualIncome * .15);
    System.out.println("Based on annual income of "+ "$ " +        
        df.format(annualIncome) + " your tax is " + "$ " +   
        df.format(taxAmount));
}
else {
    taxAmount = (annualIncome * .25);
    System.out.println("Based on annual income of "+ "$ " +   
        df.format(annualIncome) + 
        " your tax is " + "$ " + df.format(taxAmount));
}

【讨论】:

    【解决方案2】:

    您似乎放错了 if 语句的括号。 试试这段代码,它应该可以工作

    package incometax;
    
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    public class IncomeTax {
        public static void main(String[] args) {
            Scanner scnr = new Scanner(System.in);
            DecimalFormat df = new DecimalFormat ("#,###,000.00");
    
            String singleStatus = "single";
            String marriedStatus = "married";
            String maritalStatus = "";
            double annualIncome = 0;
            double taxAmount = 0;
    
            System.out.println("Please enter your martial status: ");
            maritalStatus = scnr.next();
            if (maritalStatus.compareTo(singleStatus) == 0){
                System.out.println("Please enter your annual income: ");
                annualIncome = scnr.nextDouble();
    
                if (annualIncome <= 30000){
                    taxAmount = (annualIncome * .15);
                    System.out.println("Based on annual income of "+ "$ " +        
                    df.format(annualIncome) + " your tax is " + "$ " +   
                    df.format(taxAmount));
                } else {
                    taxAmount = (annualIncome * .25);
                    System.out.println("Based on annual income of "+ "$ " +   
                    df.format(annualIncome) + 
                    " your tax is " + "$ " + df.format(taxAmount));
                }
            } else if (maritalStatus.compareTo(marriedStatus) == 0){
    
                if(annualIncome <= 30000){
                    taxAmount = (annualIncome * .12);
                    System.out.println("Based on annual income of "+ "$ "  
                    +  df.format(annualIncome) + 
                    " your tax is " + "$ " + df.format(taxAmount));
                } else {
                    taxAmount = (annualIncome * .20);
                    System.out.println("Based on annual income of "+  
                    "$ " +  df.format(annualIncome) + 
                    " your tax is " + "$ " + df.format(taxAmount));
                }
            }
        }
    }  
    

    【讨论】:

      【解决方案3】:

      一个不错的 IDE 将能够格式化您的代码以使这种嵌套问题更加明显。

      这可能不是问题的一部分,但您也有几行重复的代码,请查看方法。例如以下更干净(注意没有验证婚姻状况或年收入)

      import java.text.DecimalFormat;
      import java.util.Scanner;
      
      public class IncomeTax
      {
      
          private static final DecimalFormat DF = new DecimalFormat("#,###,000.00");
      
          private static final String SINGLE_STATUS = "single";
          private static final String MARRIED_STATUS = "married";
      
          private static final double SINGLE_LOW_RATE = .15;
          private static final double SINGLE_HIGH_RATE = .25;
      
          private static final double MARRIED_LOW_RATE = .12;
          private static final double MARRIED_HIGH_RATE = .20;
      
          private static final int LOWER_BRACKET = 30000;
      
          public static void main(String[] args)
          {
      
              Scanner scnr = new Scanner(System.in);
      
              String maritalStatus;
              double annualIncome;
      
              System.out.println(
                  "Please enter your martial status: ");
              maritalStatus = scnr.next();
      
              System.out.println("Please enter your annual income: ");
              annualIncome = scnr.nextDouble();
      
              if (SINGLE_STATUS.equalsIgnoreCase(maritalStatus))
              {
                  printTaxRate(annualIncome, SINGLE_LOW_RATE, SINGLE_HIGH_RATE);
              } else
              {
                  if (MARRIED_STATUS.equalsIgnoreCase(maritalStatus))
                  {
                      printTaxRate(annualIncome, MARRIED_LOW_RATE, MARRIED_HIGH_RATE);
                  }
              }
          }
      
          private static double calcTaxAmount(double annualIncome, double taxRate)
          {
              return annualIncome * taxRate;
          }
      
          private static void printTaxRate(double annualIncome, double lowRate, double highRate)
          {
              double taxAmount;
              if (annualIncome <= LOWER_BRACKET)
              {
                  taxAmount = calcTaxAmount(annualIncome, lowRate);
              } else
              {
                  taxAmount = calcTaxAmount(annualIncome, highRate);
              }
      
              System.out.println("Based on annual income of " + "$ "
                  + DF.format(annualIncome)
                  + " your tax is " + "$ " + DF.format(taxAmount));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-28
        • 1970-01-01
        • 2012-06-01
        • 2016-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多