1

whileループで、無効な入力を1回行った後、ループが有効な回答を返さないようにし、「エラー!無効な顧客タイプ。再試行してください」を繰り返します。プログラムを閉じるまで何度も繰り返します。初めて入力として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

番兵と一緒にdowhileループを使用することをお勧めします。これにより、コードが少なくとも1回実行されることが保証されます。

    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 に答える
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

これを試してください: (bitwise 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() ;
  }
于 2013-03-03T03:23:49.753 に答える
0

customerTypewhile ループ内ではto を割り当てません。それを最初に移動することをお勧めします。

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 に答える