我試圖接受來自用戶的輸入形式的字符。我有這個工作,但我需要檢查並確保它是6個字符(H,H,S,S,L,L)之一。我有一個while循環,但只要向它添加多個字符語句,循環會爲每個應該正確的值給出錯誤。while循環來檢查字符輸入
下面是函數:
private static char getHighLow(Scanner keyboard)
{
System.out.println("High, Low, or Sevens (H/L/S): ");
String choiceString = keyboard.next();
char choice = choiceString.charAt(0);
while (choice != 'H' || choice != 'h' || choice != 'L' || choice != 'l' || choice != 'S' || choice != 's')
{
System.out.println("You have entered an invalid entry.");
System.out.println("High, Low, or Sevens (H/L/S): ");
choiceString = keyboard.next();
}
return choice;
}
什麼是繼續檢查多個字符這樣的最佳方式?
完美。感謝您的幫助! – malibubts