我正在尝试以字符的形式接受用户的输入。我有这个工作,但我需要检查并确保它是 6 个字符之一(H、h、S、s、L、l)。我有一个 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;
}
继续检查这样的多个字符的最佳方法是什么?