我有一个程序,用户应该能够输入多少度以及什么比例(摄氏度、华氏度或开尔文)。如果用户希望退出 session ,则如果他们写“退出,退出,退出,退出,退出或退出”,则程序应该退出。我知道如何一次仅使用其中一个来执行代码,例如:
System.out.println("Enter your value, followed by the unit (C/F/K) : ");
temp=input.nextLine();
if (temp.contains("exit")) {
System.out.println("Ending");
System.exit(0);
}
我希望能够写出如下内容: if (temp.containsIgnoreCase("退出"|| "退出") )
任何人都可以帮助我提供一些如何修复代码的指南吗?我试图在这里搜索它,但没有任何效果,只有当括号内有两个不同的单词时,我才会收到错误。
最佳答案
尝试将用户输入小写,然后与 String#matches
进行比较:
if (temp.toLowerCase().matches("exit|quit")) {
System.out.println("Ending");
System.exit(0);
}
关于java - 如果字符串包含两个带有 IgnoreCase 的单词之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50999903/