“if else”中的break语句-java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20670824/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:29:35  来源:igfitidea点击:

break statement in "if else" - java

javaif-statementfor-loopinfinite-loopbreak

提问by John

I keep getting an error, ifwithout else.

我不断收到错误,if没有else.

I tried else ifas well

我也试过else if

for (;;){
        System.out.println("---> Your choice: ");
        choice = input.nextInt();
        if (choice==1)
            playGame();
        if (choice==2)
            loadGame();
        if (choice==3)
            options();
        if (choice==4)
            credits();
        if (choice==5)
            System.out.println("End of Game\n Thank you for playing with us!");
            break;
        else
            System.out.println("Not a valid choice!\n Please try again...\n");=[;'mm
    }

also if you have a better idea on how to present this code please do not hesitate :)

另外,如果您对如何呈现此代码有更好的想法,请不要犹豫:)

采纳答案by Brian Roach

Because your elseisn't attached to anything. The ifwithout braces only encompasses the single statement that immediately follows it.

因为你else没有依附于任何东西。在if没有括号只涵盖了紧随其后的单个语句。

if (choice==5)
{
    System.out.println("End of Game\n Thank you for playing with us!");
    break;
}
else
{
   System.out.println("Not a valid choice!\n Please try again...\n");
}

Not using braces is generally viewed as a bad practice because it can lead to the exact problems you encountered.

不使用大括号通常被视为不好的做法,因为它可能导致您遇到的确切问题。

In addition, using a switchhere would make more sense.

此外,使用switchhere 会更有意义。

int choice;
boolean keepGoing = true;
while(keepGoing)
{
    System.out.println("---> Your choice: ");
    choice = input.nextInt();
    switch(choice)
    {
        case 1: 
            playGame();
            break;
        case 2: 
            loadGame();
            break;
        // your other cases
        // ...
        case 5: 
            System.out.println("End of Game\n Thank you for playing with us!");
            keepGoing = false;
            break;
        default:
            System.out.println("Not a valid choice!\n Please try again...\n");
     }
 }         

Note that instead of an infinite forloop I used a while(boolean), making it easy to exit the loop. Another approach would be using break with labels.

请注意,for我使用了一个while(boolean),而不是无限循环,以便轻松退出循环。另一种方法是使用带标签的中断。

回答by clcto

The issue is that you are trying to have multiple statements in an ifwithout using {}. What you currently have is interpreted like:

问题是您试图在if不使用{}. 您目前拥有的解释如下:

if( choice==5 )
{
    System.out.println( ... );
}
break;
else
{
    //...
}

You really want:

你真的想要:

if( choice==5 )
{
    System.out.println( ... );
    break;
}
else
{
    //...
}

Also, as Farce has stated, it would be better to use else iffor all the conditions instead of ifbecause if choice==1, it will still go through and check if choice==5, which would fail, and it will still go into your else block.

此外,正如 Farce 所说,最好else if用于所有条件而不是if因为 if choice==1,它仍然会通过并检查 if choice==5,这会失败,它仍然会进入你的 else 块。

if( choice==1 )
    //...
else if( choice==2 )
    //...
else if( choice==3 )
    //...
else if( choice==4 )
    //...
else if( choice==5 )
{
    //...
}
else
    //...


A more elegant solution would be using a switchstatement. However, breakonly breaks from the most inner "block" unless you use labels. So you want to label your loop and break from that if the case is 5:

更优雅的解决方案是使用switch语句。但是,break除非您使用标签,否则只会从最内部的“块”中断。所以你想标记你的循环并在 case 为 5 时中断:

LOOP:
for(;;)
{
    System.out.println("---> Your choice: ");
    choice = input.nextInt();
    switch( choice )
    {
        case 1:
            playGame();
            break;
        case 2:
            loadGame();
            break;
        case 2:
            options();
            break;
        case 4:
            credits();
            break;
        case 5:
            System.out.println("End of Game\n Thank you for playing with us!");
            break LOOP;
        default:
            System.out.println( ... );
    }
}

Instead of labeling the loop, you could also use a flag to tell the loop to stop.

除了标记循环之外,您还可以使用标志来告诉循环停止。

bool finished = false;
while( !finished )
{
    switch( choice )
    {
        // ...
        case 5:
            System.out.println( ... )
            finished = true;
            break;
        // ...
    }
}

回答by Alan J. Robinson

The "break" command does not work within an "if" statement.

“break”命令在“if”语句中不起作用。

If you remove the "break" command from your code and then test the code, you should find that the code works exactly the same without a "break" command as with one.

如果您从代码中删除“break”命令,然后测试代码,您会发现代码的工作方式与没有“break”命令的代码完全相同。

"Break" is designed for use inside loops (for, while, do-while, enhanced for and switch).

“Break” 设计用于内部循环(for、while、do-while、enhanced for 和 switch)。