Java if 语句可以嵌套在 while 循环中吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18265864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can if statements be nested in a while loop?
提问by kehmoto1
I've been working with code that uses a do-while loop, and I wanted to add an if else statement into that loop. The do-while loop checks to see what text the user enters and will finish if the word 'exit' is entered.
我一直在处理使用 do-while 循环的代码,我想在该循环中添加一个 if else 语句。do-while 循环检查用户输入的文本,如果输入“退出”这个词,它将完成。
public static void main(String[] args) {
String endProgram = "exit";
String userInput;
java.util.Scanner input = new java.util.Scanner(System.in);
do {
userInput = input.nextLine();
if ( !userInput.equalsIgnoreCase(endProgram) ) {
System.out.printf("You entered the following: %s", userInput);
} else
} while ( !userInput.equalsIgnoreCase(endProgram) );
}
When I try to compile this code, I get an error from the command prompt saying:
当我尝试编译此代码时,我从命令提示符处收到一条错误消息:
SentinelExample.java:20: error: illegal start of expression
} while ( !userInput.equalsIgnoreCase(endProgram) );
^
SentinelExample.java:22: error: while expected
}
^
SentinelExample.java:24: error: reached end of file while parsing
}
^
When I remove the if else statement in the loop, the program compiles fine. Is there something wrong with the syntax in my program? Or is it not possible to put an if else statement in a while loop?
当我删除循环中的 if else 语句时,程序编译正常。我的程序中的语法有问题吗?或者不能在 while 循环中放置 if else 语句?
采纳答案by vee
Check your code here you are missing else
block:
在此处检查您的代码,您缺少else
块:
userInput = input.nextLine();
if ( !userInput.equalsIgnoreCase(endProgram) ) {
System.out.printf("You entered the following: %s", userInput);
} else
This is the reason for the compilation error. You can fix this by either removing the else
altogether or if you want to add something in it then do the following:
这就是编译错误的原因。您可以通过else
完全删除或如果您想在其中添加某些内容来解决此问题,请执行以下操作:
userInput = input.nextLine();
if ( !userInput.equalsIgnoreCase(endProgram) ) {
System.out.printf("You entered the following: %s", userInput);
} else {
// Do something
}
And to answer your questions, yes it's perfectly valid to nest if
statements in a while
loop.
为了回答您的问题,是的,将if
语句嵌套在while
循环中是完全有效的。
回答by rocketboy
You are missing {}
to close the else block. Instead of
您缺少{}
关闭 else 块。代替
do{
if(){
}else
}while()
use
用
do{
if(){
}else{
}
}while()
回答by Asaf Nevo
That because of your else statment that does nothing but make the complier think that the while condition is under the else condition and that wrong. Just remove it
那是因为你的 else 语句只会让编译器认为 while 条件在 else 条件下并且是错误的。删除它
回答by Abhishek
Its quite possible:
它很有可能:
public static void main(String[] args) {
String endProgram = "exit";
String userInput;
java.util.Scanner input = new java.util.Scanner(System.in);
do {
userInput = input.nextLine();
if ( !userInput.equalsIgnoreCase(endProgram) ) {
System.out.printf("You entered the following:"+userInput);// use plus ssign instead of %s
}
} while ( !userInput.equalsIgnoreCase(endProgram) );
}
回答by usama iftikhar
package second;
public class Even {
public static void main(String[] args) {
int a=0;
while (a<=100)
{
if (a%2==0)
{
System.out.println("The no is EVEN : " + a);
}
else
{
System.out.println("The num is ODD : " + a);
}
} } }
why this isn't working rightly, it only display the 0, with even !
为什么这不能正常工作,它只显示 0,甚至 !