Java 如何使用字符串变量作为循环的条件
我试图为我的实践准备一个程序流程图,我遇到以下情况,用户需要输入字符串值作为输入“是”或“Y”和“否”或“N”根据它的输入,应用程序将从某个点终止或重新启动,直到现在我脑海中有一个例子Java 如何使用字符串变量作为循环的条件,java,Java,我试图为我的实践准备一个程序流程图,我遇到以下情况,用户需要输入字符串值作为输入“是”或“Y”和“否”或“N”根据它的输入,应用程序将从某个点终止或重新启动,直到现在我脑海中有一个例子 public class ApplicationName { public static void main(String args[]) { String restartOperation; do { restartOperation = Con
public class ApplicationName {
public static void main(String args[]) {
String restartOperation;
do {
restartOperation = Confirm_Before_Exit();
} while (!restartOperation.equals("No"));
//Rest of code
}
public static void Some_Operation() {
//Executed when called before closing application
}
public static String Confirm_Before_Exit() {
Scanner inputData = new Scanner(System.in);
String answer;
System.out.println("Do you want to perform another operation ?" + " " + "Y" + " " + "N");
answer = inputData.nextLine();
switch (answer) {
case "Y":
case "Yes":
Some_Operation();
break;
default:
System.out.println("Good Bye !");
}
return answer;
}
}
直到用户没有以“否”的形式输入时,这才起作用,但如果输入“N”或小的“N”甚至“否”,则显然不起作用,但对于计时,我只尝试将“否”和“N”作为输入值。我会这样做。您将答案设置为小写,以考虑诸如是、否等情况。然后指定n和否。默认值应为“全部覆盖”
answer = inputData.nextLine().toLowerCase();
switch (answer) {
case "y":
case "yes":
Some_Operation();
break;
case "n":
case "no":
System.out.println("Good Bye !");
break;
default:
System.out.println("Not a valid reponse");
}
将do while更改为以下内容:
do {
restartOperation = Confirm_Before_Exit();
} while (!restartOperation.equalsIgnoreCase("No") && !restartOperation.equalsIgnoreCase("n"));
您可能希望使用Regex进行文本匹配(它包含在默认的Java JDK中,而不是外部库中)。它非常强大 在正则表达式语法中定义要搜索的指针,例如:
^no?$
它匹配以n
开始(^
)的文本,然后最后(?
)后跟o
(但不需要在那里),然后文本结束($
)。还有一个不区分大小写的匹配标志/i
。您可以在regex101.com上尝试Regex
。试试这个:
好的,如何在Java中使用这些东西?相当简单:
String input = ...
Pattern pattern = Pattern.compile("^no?$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
// User want's to cancel
...
} else {
// User want's to continue
...
}
更多信息请访问,和
您也可以将其放在自己的方法中,使其使用更简单,更易于阅读:
private boolean doesUserWantToCancel(final String textInput) {
Pattern pattern = Pattern.compile("^no?$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(textInput);
return matcher.find();
}
然后,您只需调用输入的方法:
...
} while (!doesUserWantToCancel(restartOperation));
...
如果我正确理解了您的问题,您希望在重新启动操作时运行
在退出之前确认()
这就是我要做的。您不应该使用字符串重新启动操作
,而应该使用布尔重新启动操作
。这样,你要么处理对的问题,要么处理错的问题
在Confirm\u Before\u Exit()。我添加了两个布尔值应答
和invalidansower
如果要执行另一个操作,您希望您的答案为true。如果它为false,它将退出
如果用户输入错误,inValideAnswer
将被设置为true,他们将得到一个错误,提示回答无效,并将被重新提示
public class ApplicationName {
public static void main(String args[]) {
boolean restartOperation;
do {
restartOperation = Confirm_Before_Exit();
} while (restartOperation);
//Rest of code
}
public static void Some_Operation() {
//Executed when called before closing application
}
public static boolean Confirm_Before_Exit() {
Scanner inputData = new Scanner(System.in);
boolean answer;
boolean validAnswer;
while(inValideAnswer){
System.out.println("Do you want to perform another operation ?" + " " + "Y" + " " + "N");
string userInput = inputData.nextLine().toLowerCase();
switch (userInput) {
case "y":
case "yes":
Some_Operation();
answer = true;
inValideAnswer = false;
break;
case "n":
case "no":
answer = false;
inValideAnswer = false;
System.out.println("Good Bye !");
break;
default:
System.out.println("Not a valid reponse");
inValideAnswer = true;
}
}
return answer;
}
}
这是一个错误的答案&不起作用,因为在do-while
循环中,他正在检查No
。这就是需要改变的地方。它再次进入无限循环,即使输入“n”或“no”,也会不断地询问。Mehulchada更新了答案。|
需要替换为&&
。试试看。它现在可以工作了没有其他方法不使用Java API而只使用Java?Regex是本机Java的一部分。它是纯Java,您可以在没有任何外部库或其他东西的情况下使用它。把上面的代码放到你的程序中——它会工作的。