“if else”中的break语句 - java

Posted

技术标签:

【中文标题】“if else”中的break语句 - java【英文标题】:break statement in "if else" - java 【发布时间】:2014-01-07 09:06:50 【问题描述】:

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

我也试过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
    

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

【问题讨论】:

Java 不是 Python。您不能仅仅因为它们具有相同的缩进就期望两行在同一个块中... 即使这样也不能完全解决问题。所有if 语句都需要与else 连接。 不应该是:“else without if”吗? 【参考方案1】:

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

如果您从代码中删除“break”命令,然后对代码进行测试,您应该会发现没有“break”命令的代码与使用“break”命令的代码完全相同。

“Break”专为在循环内部使用(for、while、do-while、增强的 for 和 switch)而设计。

【讨论】:

如果 if 语句不在控制结构循环/开关中,则“break”命令在“if”语句中不起作用。我认为这就是混乱所在?如果它更容易理解,想象一下在 if 语句中使用“继续”语句。 @erez 这和 yoink 一样老,但 Alan 是正确的,因为 break 语句不会从 IF 语句中跳出。它跳出一个循环,基本上,你说那是错误的答案,然后重复艾伦所说的,只是措辞不同。 Alan 并不是说​​它不会在 if 中运行,他的意思是它不会脱离 if。阅读最后一部分:“break”是为在循环内部使用而设计的(for、while、do-while、增强的 for 和 switch)。” Derp. break 在这种情况下有效,因为它将打破无休止的 for(;;) 循环。【参考方案2】:

因为您的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");

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

此外,在这里使用switch 会更有意义。

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");
     
          

请注意,我使用while(boolean) 代替了无限的for 循环,这样可以很容易地退出循环。另一种方法是使用带标签的中断。

【讨论】:

您要记下如何跳出循环,因为开关中的break; 只会跳出开关。【参考方案3】:

问题是您试图在 if 中包含多个语句而不使用 。 您当前拥有的内容被解释为:

if( choice==5 )

    System.out.println( ... );

break;
else

    //...

你真的想要:

if( choice==5 )

    System.out.println( ... );
    break;

else

    //...

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

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

    //...

else
    //...

更优雅的解决方案是使用switch 语句。但是,break 只会从最内部的“块”中断,除非您使用标签。因此,如果案例为 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( ... );
    

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

bool finished = false;
while( !finished )

    switch( choice )
    
        // ...
        case 5:
            System.out.println( ... )
            finished = true;
            break;
        // ...
    

【讨论】:

【参考方案4】:

使用switch 语句。 使用break 2 突破多个级别。

https://www.php.net/manual/en/control-structures.break.php

【讨论】:

以上是关于“if else”中的break语句 - java的主要内容,如果未能解决你的问题,请参考以下文章

java,多层for()循环,if()else嵌套分别用啥替代?

分支语句 if...else switch..case...break

Java中的switch分支注意点

java switch的应用

Python中的if else 和while else的用法

c语言小程序 关于for循环,if else和break,帮忙改下错谢谢