【问题标题】:How to break out of loop when case is selected选择案例时如何跳出循环
【发布时间】:2014-10-30 03:56:23
【问题描述】:

我有一个包含 4 个案例的代码,我试图打破循环,如果选择了“f”案例。然后从那个案例中选择。当我尝试使用 break over 30 错误执行 if 语句时,但当我把它拿走时,代码很好。

String one = "";
boolean yea = true;   
Scanner sw = new Scanner(System.in);
while (yea == true)
{
    System.out.print(MENU);
    one  = sw.next();
    char choice  = one.charAt(0);
    switch(choice)
    {
        case 'f':
            friendsList();
            break; 
        case 'w':
            wall();
            break;
        case 'p':
            network();
            break;

        case 'q' : 
            yea = false;
            break; 
        default:
            System.out.println("Error: You have entered " + choice + 
            ". Please try again");

    }
}
if (case == 'f')
    {
    break;
    }
}

【问题讨论】:

  • 好吧,花括号不匹配,但您为什么不发布错误消息?
  • 我认为打破while循环的if语句需要在while循环内。

标签: java while-loop switch-statement case


【解决方案1】:

您将使用 Java label(请参阅此名为 BreakWithLabelDemo.java 的代码示例)告诉您的代码到哪里去 break

myloop:
    while ( true ){
        switch( choice ){
            case 'f':
                friendsList();
                break myloop;
        }
    }

【讨论】:

  • 链接可能对 API 或示例有所帮助
  • 对不起,我在粘贴代码示例之前提交了它
【解决方案2】:

对于您的实现,在进入 switch 语句之前打破特定情况是有意义的。例如:

char choice  = one.charAt(0);

if (choice == 'f') break;

switch(choice)

这似乎是一种非常简单的退出 while 循环的方法,而不会与 switch 语句的 break 语句发生冲突。

或者,如果在choice'f' 时仍需要调用friendsList 方法,则可以将该if 语句移到switch 语句之后。

注意:您还应该删除代码示例底部的 if 语句。

【讨论】:

    【解决方案3】:
    if (case == 'f')
    

    此语句中的大小写是什么?你应该用选择替换它。

    if (choice == 'f')
    

    【讨论】:

      【解决方案4】:

      你需要把 if 放在 while 循环中。

          String one = "";
          boolean yea = true;   
          Scanner sw = new Scanner(System.in);
          while (yea == true)
          {
              System.out.print(MENU);
              one  = sw.next();
              char choice  = one.charAt(0);
              switch(choice)
              {
                  case 'f':
                      friendsList();
                      break;
                  case 'w':
                      wall();
                      break;
                  case 'p':
                      network();
                      break;
      
                  case 'q' : 
                      yea = false;
                      break; 
                  default:
                      System.out.println("Error: You have entered " + choice + 
                      ". Please try again");
      
              }
              if (choice == 'f')
              {
              break;
              }
      
          }
      

      【讨论】:

        【解决方案5】:

        if语句要移到while循环里面才有效,if语句里面的case要改成choice。

        所以

          While(yea==true)
          {
                System.out.print(MENU);
                one  = sw.next();
                char choice  = one.charAt(0);
        
                if(choice == 'F')
                {
                      break;
                }
                switch(choice)
                {
                  //cases          
                }
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-28
          • 2014-10-17
          • 2010-12-22
          • 2012-06-28
          • 2016-01-19
          • 1970-01-01
          • 1970-01-01
          • 2020-08-08
          • 1970-01-01
          相关资源
          最近更新 更多