英文:
While loop doesn't break if there is no input
问题
我想要在用户不输入任何值时退出while循环。但是,当用户没有输入任何内容时,什么也不会发生,似乎也不会退出while循环,因为它不会打印结束语句。
我尝试创建一个if语句,如果输入的长度等于0,那么valid变为false。以下是我的完整代码。
boolean valid = true;
String exp = " ";
while(valid) {
System.out.println("CS211 Spring Quarter 2023");
System.out.print("Enter a math expression : ");
exp = s.next();
if(exp.length()==0) {
valid = false;
}
// 只有当表达式有效时,代码才会继续将其转换为后缀表达式,并评估表达式
if(evaluationofexpression.evalexpress(exp)) {
System.out.println();
String postfix = infixtopostfix.infix2postfix(exp);
String evaluated = String.valueOf(evalpostfix.evaluatePostfix(postfix));
System.out.println("infix expression:" + exp);
System.out.println("postfix expression:" + postfix);
System.out.println("evaluated expression:" + evaluated);
System.out.println();
}
// 如果表达式为false,它将要求您重试并重复代码
else {
System.out.println("Try again");
System.out.println();
}
}
System.out.println("bye");
英文:
I want to leave the while loop if the user doesn't input any value. But, when the user doen't input anything, nothing happens, and it also doesn't seem to exit the while loop, because it doen't print the end statement.
I tried to make an if statement that says if the length of the input is equal to 0, valid becomes false. Here's my full code.
boolean valid = true;
String exp =" ";
while(valid) {
System.out.println("CS211 Spring Quarter 2023");
System.out.print("Enter a math expression : ");
exp = s.next();
if(exp.length()==0) {
valid = false;
}
//only when the expression is valid, the code proceeds to convert it to postfix, and evaluate the expression
if(evaluationofexpression.evalexpress(exp)) {
System.out.println();
String postfix = infixtopostfix.infix2postfix(exp);
String evaluated = String.valueOf(evalpostfix.evaluatePostfix(postfix));
System.out.println("infix expression:"+ exp);
System.out.println("postfix expression:"+ postfix);
System.out.println("evaluated expression:"+ evaluated);
System.out.println();
}
//if the expression is false, it will ask you to try again and repeat the code
else {
System.out.println("Try again");
System.out.println();
}
}
System.out.println("bye");
答案1
得分: 1
The Scanner class uses a delimiter to capture the values returned by the next method.
The JavaDoc for Scanner states the following.
> A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. ...
> ... The default whitespace delimiter used by a scanner is as recognized by Character.isWhitespace()
So, I presume you're expecting that when the enter-key is pressed, a newline should terminate the process.
This is not the case—since the Scanner will have not captured a delimiter, yet.
If you're looking to capture the entire line provided by the user, and not just one value at a time, use the Scanner#nextLine method.
This will be effective when checking for a length of 0.
Thus, assigning false to valid, and exiting the loop upon the re-iteration evaluation.
英文:
The Scanner class uses a delimiter to capture the values returned by the next method.
The JavaDoc for Scanner states the following.
> A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. ...
>
> ... The default whitespace delimiter used by a scanner is as recognized by Character.isWhitespace()
So, I presume you're expecting that when the enter-key is pressed, a newline should terminate the process.
This is not the case—since the Scanner will have not captured a delimiter, yet.
If you're looking to capture the entire line provided by the user, and not just one value at a time, use the Scanner#nextLine method.
This will be effective when checking for a length of 0.
Thus, assigning false to valid, and exiting the loop upon the re-iteration evaluation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论