如何确保用户输入特定字符串?

时间:2017-03-25 17:30:04

标签: java string encryption equality negation

我正在尝试询问用户是否要加密或解密消息,我知道我需要使用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")的语法。 (显然,我所说的不对)。

1 个答案:

答案 0 :(得分:1)

只需将while loop条件更改为:

while(!userInput.equals("encrypt") && !userInput.equals("decrypt"))

另外,如果你想忽略大小写:

while(!userInput.equalsIgnoreCase("encrypt") && !userInput.equalsIgnoreCase("decrypt"))