如何确保用户输入的是特定字符串?
How to make sure a user is entering a specific string?
我正在尝试询问用户是否要加密或解密消息,我知道我需要使用 userInput.equals("encrypt")
,但我想使用带有 != 的 while 循环。
while(userInput.!equals("encrypt") || userInput.!equals("decrypt")){
System.out.println("Please try again. Check spelling, and that you typed either 'encrypt' or 'decrypt'.);
userInput = scan.nextLine().toLowerCase();
}
而且我不知道 userInput.!equal("x")
的语法。 (很明显,我放的不对)
只需将 while loop
条件更改为:
while(!userInput.equals("encrypt") && !userInput.equals("decrypt"))
此外,如果您想忽略大小写:
while(!userInput.equalsIgnoreCase("encrypt") && !userInput.equalsIgnoreCase("decrypt"))
我正在尝试询问用户是否要加密或解密消息,我知道我需要使用 userInput.equals("encrypt")
,但我想使用带有 != 的 while 循环。
while(userInput.!equals("encrypt") || userInput.!equals("decrypt")){
System.out.println("Please try again. Check spelling, and that you typed either 'encrypt' or 'decrypt'.);
userInput = scan.nextLine().toLowerCase();
}
而且我不知道 userInput.!equal("x")
的语法。 (很明显,我放的不对)
只需将 while loop
条件更改为:
while(!userInput.equals("encrypt") && !userInput.equals("decrypt"))
此外,如果您想忽略大小写:
while(!userInput.equalsIgnoreCase("encrypt") && !userInput.equalsIgnoreCase("decrypt"))