【问题标题】:While loop returns errors over and over after the first errorWhile 循环在第一个错误之后一遍又一遍地返回错误
【发布时间】:2013-03-03 03:16:30
【问题描述】:

在一个 while 循环中,我使循环在一次无效输入后不会返回有效答案,并重复“错误!无效的客户类型。再试一次。”一遍又一遍,直到我关闭程序。如果我第一次输入 R 或 C 作为输入,它可以正常工作。当我输入其他任何内容时,我收到错误消息“错误!客户类型无效。再试一次。”就像我应该是故意的。但是,在输入 r 或 c 的错误之后再次给我错误,并且我所做的任何输入都会一遍又一遍地返回错误消息,直到我关闭程序。有人可以告诉我导致这种情况的代码有什么问题吗?

public static String getValidCustomerType(Scanner sc)
{
    String customerType = ("");
      System.out.println("Enter the Customer Type");
      customerType = sc.next() ;
      boolean isValid = false;
      while (isValid == false)
      {
       if (customerType.equalsIgnoreCase("R")|customerType.equalsIgnoreCase("C") ) 
       {
         isValid = true;
       }
       else
       {
          System.out.println("Error! Invalid customer type. Try again ");
       }   
       sc.nextLine() ;
      }
   return customerType ;
} 

【问题讨论】:

  • 循环中每次都需要指定customerType

标签: java validation while-loop


【解决方案1】:

您没有在 while 循环内分配给 customerType。最好把它移到开头。

public static String getValidCustomerType(Scanner sc)
{
    String customerType = ("");

      boolean isValid = false;
      while (isValid == false)
      {
          System.out.println("Enter the Customer Type");
          customerType = sc.nextLine() ;
       if (customerType.equalsIgnoreCase("R")|customerType.equalsIgnoreCase("C") ) 
       {
         isValid = true;
       }
       else
       {
          System.out.println("Error! Invalid customer type. Try again ");
       }   
      }
   return customerType ;
} 

【讨论】:

    【解决方案2】:

    我认为您必须在 while 循环内移动输入调用。否则 customerType 变量总是相同的。

    public static String getValidCustomerType(Scanner sc)
    {
        String customerType = ("");
          System.out.println("Enter the Customer Type");
          // move this to while loop
          //customerType = sc.next() ;
          boolean isValid = false;
          while (isValid == false)
          {
           // get input here
           customerType = sc.next() ;
           if (customerType.equalsIgnoreCase("R")|customerType.equalsIgnoreCase("C") ) 
           {
             isValid = true;
           }
           else
           {
              System.out.println("Error! Invalid customer type. Try again ");
           }   
           sc.nextLine() ;
          }
       return customerType ;
    } 
    

    【讨论】:

      【解决方案3】:

      试试这个:|(按位 OR) 与 || 不同,后者是一个布尔 OR 运算符。其次,您不会再次分配 customerType - 修复如下。

      while (isValid == false)
        {
         if (customerType.equalsIgnoreCase("R")||customerType.equalsIgnoreCase("C") ) 
         {
           isValid = true;
         }
         else
         {
            System.out.println("Error! Invalid customer type. Try again ");
         }   
          customerType = sc.nextLine() ;
        }
      

      【讨论】:

        【解决方案4】:

        我建议将 do while 循环与哨兵一起使用。这保证了代码至少执行once

            public static String getValidCustomerType(Scanner sc) {
                String customerType;
                boolean isValid = false;
        
                System.out.println("Enter the Customer Type");
        
                do {
                    customerType = sc.next();
        
                    if (customerType.equalsIgnoreCase("R")|customerType.equalsIgnoreCase("C") )  {
                        isValid = true;
                    } else {
                        System.out.println("Error! Invalid customer type. Try again ");
                    }   
                } while(!isValid);
        
                return customerType ;
            } 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-24
          • 1970-01-01
          • 2021-01-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-16
          • 2017-02-10
          相关资源
          最近更新 更多