1

在一个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 ;
} 
4

4 回答 4

0

我认为您必须在 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 ;
} 
于 2013-03-03T03:21:55.103 回答
0

试试这个:(|按位或)与||布尔或运算符不同。其次,您不再分配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() ;
  }
于 2013-03-03T03:23:49.753 回答
0

您没有分配到customerTypewhile 循环内。最好还是把它移到开头。

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 ;
} 
于 2013-03-03T03:26:00.763 回答
0

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

    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 ;
    } 
于 2013-03-03T03:46:24.900 回答