0
我一直在閱讀驗證的用戶輸入,如果它不是一個數字或者如果輸入不是一個字符,但我如何檢查用戶輸入是我定義的東西他們?驗證輸入字使用Scanner.hasNext()
System.out.println("What meal would you like to eat?"
+ " (appetizer, soup, salad, main, or dessert)");
String meal = console.next();
我看到
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a vowel, lowercase!");
while (!sc.hasNext("[aeiou]")) {
System.out.println("That's not a vowel!");
sc.next();
}
,但是當我改變
(!sc.hasNext("[aeiou]"))
到
(!sc.hasNext("[appetizer, soup, salad, main, dessert]"))
你不能組成東西。字符串是什麼意思(文檔說)? – user2864740
因爲這不是有效的模式。請參閱:[hasNext(String pattern)](http://www.tutorialspoint.com/java/util/scanner_hasnext_string.htm) – Li357
hmmm嘗試'sc.hasNext(「開胃菜|湯|沙拉|主|甜點」)' – niceman