2015-09-30 136 views
3

我最近开始学习Java,在测试某些东西时遇到了问题。这可能是一个非常简单的问题,但我似乎无法解决它。 这是我的代码:如果在while循环中,不能跳出while循环

int firstj = 1; 

    if (firstj == 1) { 
     String choice = "Type a number between 1 and 4"; 
     System.out.println(choice); 
     while (true) { 

      if (firstj == 1) { 
       Scanner third = new Scanner(System.in); 
       String thirdch = third.nextLine(); 

       while (true) { 

        if (thirdch.equals("1")) { 
         System.out.println("Show choice and accept input again "); 
         System.out.println(choice); 
         break; 
        } else if (thirdch.equals("2")) { 
         System.out.println("Show choice and accept input again "); 
         System.out.println(choice); 
         break; 
        } else if (thirdch.equals("3")) { 
         System.out.println("Show choice and accept input again "); 
         System.out.println(choice); 
         break; 
        } 

        else if (thirdch.equals("4")) { 
         // I need this to break the loop and move on to the 
         // "Done." string 
         break; 
        } 

        else { 
         System.out.println("Type a number between 1 and 4"); 
         thirdch = third.nextLine(); 
        } 
       } 

      } 
     } 
    } 
    String done = "Done"; 
    System.out.println(done); 

我想让它这样,当你键入1,2或3,你得到的字符串,告诉您再次输入一个号码,接受用户的输入,而当你键入4循环会中断并转到完成的字符串。如果你能用简单的代码帮助我解决这个问题,我将不胜感激,因为我不知道任何更先进的东西。

+0

你的意思是你想突破* outer *循环吗? –

+0

你正在打破内在,而不是外循环。所以虽然(真)永远是真的。 – Casey

+0

“break”只能让你摆脱1循环。你最根本的问题是无限循环。有一个无限循环已经够糟了,你实际上有2个。现在是重构代码的时候了。 –

回答

7

您可以标记循环,然后使用break语句中的标签指定要跳出哪个循环,例如,

outer: while (true) { 
    while (true) { 
    break outer; 
    } 
} 
1

这是不是很从描述清楚,但continue会让你跳到循环结束,继续与循环的其余部分,你可以使用标志来控制,如果你想退出多个级别。

5

爆发嵌套循环的最具可扩展性的方法是将整个事物放入函数中并使用return

在Java中打破标签是另一种选择,但它可能使代码变得脆弱:你受到一些顽固的重构者的摆布,他们可能觉得倾向于将“打破标签”移动到幸福地不知道后果;编译器无法警告这些恶意软件。

+1

也许你可以举一些脆弱性的例子来提防?这种'不确定的恶劣'在被咬伤之前很难发现。 –

+0

我认为Bathsheba只是试图指出,当添加更多的循环并且代码变得更大和更复杂时,break语句的这种用法很难遵循,我完全同意他的观点。 –

+0

@ChristopherYang我不会不同意:我实际上还记得上次我用标签打破时间;我认为通常有更简单的方法来构建代码以避免它。我的观点是,对那些不熟悉它创建的问题的人来说,描述代码是脆弱的是什么意思是很有用的。 –

2

你可以使用一个锁停下来,这样的:

public static void main(String[] args) throws Exception { 
    int firstj = 1; 
    if (firstj == 1) { 
     boolean lock = true; 
     while (lock) { 

      if (firstj == 1) { 
       Scanner third = new Scanner(System.in); 

       while (lock) { 

        System.out.println("Type a number between 1 and 4"); 
        String thirdch = third.nextLine(); 
        switch (thirdch) { 
        case "1": 
         System.out 
           .println("Show choice and accept input again "); 
         break; 
        case "2": 
         System.out 
           .println("Show choice and accept input again "); 
         break; 
        case "3": 
         System.out 
           .println("Show choice and accept input again "); 
         break; 
        case "4": 
         lock = false; 
         break; 
        } 

       } 
       third.close(); 

      } 
     } 
    } 
    String done = "Done"; 
    System.out.println(done); 
} 

我希望它能帮助。