在一個while循環中,我使循環在一個無效輸入後不會返回有效答案,並重復「錯誤!無效的客戶類型,請重試。一遍又一遍,直到我關閉程序。如果我第一次輸入R或C作爲輸入,它可以正常工作。當我輸入其他內容時,我收到錯誤消息「錯誤!無效的客戶類型,請重試。」就像我應該是故意的那樣。然而,在輸入r或c後,錯誤再次給我提供了錯誤,並且我所做的任何輸入都會一遍又一遍地返回錯誤消息,直到關閉程序。有人可以告訴我什麼是錯的,在我的代碼造成這種情況?while循環在第一個錯誤之後反覆返回錯誤
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 – Steven 2013-03-03 03:20:18