【问题标题】:How to make sure a user is entering a specific string?如何确保用户输入的是特定字符串?
【发布时间】:2017-03-25 17:30:04
【问题描述】:

我试图询问用户是否要加密或解密消息,并且我知道我需要使用 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(!userInput.equal("encrypt") || !userInput.equal("decrypt"))前面加上感叹号,这样输出就反转了
  • 是的,谢谢。有用。再过几分钟我无法接受答案
  • 等等。它总是不等于“加密”或不等于“解密”。那不应该是&&吗?

标签: java string encryption equality negation


【解决方案1】:

只需将while loop 条件更改为:

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

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

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

【讨论】:

  • 但必须是&& 而不是||
猜你喜欢
  • 2021-02-25
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多