简体   繁体   English

在 Java 中没有出现循环

[英]While loop not breaking out in Java

I have written this switch case program in Java.我在 Java 中编写了这个 switch case 程序。 However, the while loop is not breaking out.但是,while 循环没有中断。 Here's the code:这是代码:

import java.util.Scanner;
public class exam001 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int marks;
        while(true) {
            System.out.println("This is a gade checker program");
            System.out.println("Enter the marks from 0 to 100: ");
            System.out.println("Enter the marks: ");
            marks = scanner.nextInt();
            int grade = marks / 10;
            if (marks > 100) {
                System.out.println("Please enter the marks between the limit assigned");
            }
            else {
                switch(grade) {
                case 10:
                case 9:
                    System.out.println("Your grade is A");
                    break;
                case 8:
                case 7:
                    System.out.println("Your grade is B");
                    break;
                case 6:
                    System.out.println("Your grade is C");
                    break;
                case 5:
                case 4:
                    System.out.println("Your grade is D");
                    break;
                    
                default:
                    System.out.println("Your grade is E");
                    break;
                }
            }
        }
    }
}

I dont know, why the break function is not working in this loop.. Please help me..我不知道,为什么 break function 在这个循环中不起作用..请帮帮我..

Use labels to break out of the while instead of the switch :使用标签break while不是switch

loop:
while(true) {
    // ...
    // later:
    switch (..) {
        case ..:
            break loop;
    } 
}

See this tutorial for details: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html有关详细信息,请参阅本教程: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

It's never going to break out of it because the break is referring to the switch statement它永远不会摆脱它,因为break指的是switch语句

Alternatively you could ask the user whether to break out of the loop or not或者,您可以询问用户是否跳出循环

public class exam001 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int marks;
        boolean again = true;
        while(again) {
            ...
            marks = scanner.nextInt();
            int grade = marks / 10;
            if (marks > 100) {
               ...
            } else {
                switch(grade) {
                   ...
                }
            }

            System.out.println("Do you want to insert another grade (y/n): ");
            String answer;
            do {
               String answer = scanner.nextLine();
            } while (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("n")); 
            again = answer.equalsIgnoreCase("y");
            
        }
    }
}

Your break statement works inside the switch statement only so to break outside of the loop Try this:您的break语句仅在switch语句内起作用,以便在循环之外中断试试这个:

import java.util.Scanner;
public class exam001 {
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int marks;
    while(true) {
        Boolean shouldBreak = false;
        System.out.println("This is a gade checker program");
        System.out.println("Enter the marks from 0 to 100: ");
        System.out.println("Enter the marks: ");
        marks = scanner.nextInt();
        int grade = marks / 10;
        if (marks > 100) {
            System.out.println("Please enter the marks between the limit assigned");
        }
        else {
            switch(grade) {
                case 10:
                case 9:
                    System.out.println("Your grade is A");
                    shouldBreak = true;
                    break;
                case 8:
                case 7:
                    System.out.println("Your grade is B");
                    shouldBreak = true;
                    break;
                case 6:
                    System.out.println("Your grade is C");
                    shouldBreak = true;
                    break;
                case 5:
                case 4:
                    System.out.println("Your grade is D");
                    shouldBreak = true;
                    break;
                
                default:
                    System.out.println("Your grade is E");
                    shouldBreak = true;
                    break;
                }
                if (shouldBreak){
                    break;
                }
            }
        }
    }
}
boolean shouldBreak = false;
while (!shouldBreak) {
    System.out.println("This is a gade checker program");
    System.out.println("Enter the marks from 0 to 100: ");
    System.out.println("Enter the marks: ");
    marks = scanner.nextInt();
    int grade = marks / 10;
    if (marks > 100) {
        System.out.println("Please enter the marks between the limit assigned");
    } else {
       switch(grade) {
          case 10:
          case 9:
             System.out.println("Your grade is A");
             shouldBreak = true;
             break;
          case 8:
          case 7:
             System.out.println("Your grade is B");
             shouldBreak = true;
             break;
          case 6:
             System.out.println("Your grade is C");
             shouldBreak = true;
             break;
          case 5:
          case 4:
             System.out.println("Your grade is D");
             shouldBreak = true;
             break;
          default:
             System.out.println("Your grade is E");
             shouldBreak = true;
      }
   }
}

Suggestion: you should set a varaiable inside the while statement so whenever you want to breakout just change the variable, it's just cleaner way instead of breaking out the switch statement and then checking using an if statement.. the while is using an if anyway(so why not use it?)建议:您应该在 while 语句中设置一个变量,这样每当您想要突破时只需更改变量,这只是更简洁的方式,而不是打破 switch 语句然后使用 if 语句进行检查.. while 无论如何都使用 if(那么为什么不使用它呢?)

as @Ole VV comment in your question.正如您的问题中的@Ole VV 评论。 the problem is that you're breaking out the switch statement each statement has it own level.问题是你打破了 switch 语句,每个语句都有自己的级别。 it's like variables and global variables, a variable inside a function is overriding the global variable.就像变量和全局变量一样,function 中的变量覆盖了全局变量。 so your switch statement is overriding the break of the while loop.所以你的 switch 语句覆盖了 while 循环的中断。 you can't use "break" to break the while loop unless you'll use it directly in the while level outside the switch method.除非您在 switch 方法之外的 while 级别中直接使用它,否则您不能使用“break”来中断 while 循环。


import java.util.Scanner;
public class exam001 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int marks;
        while(true) {
            System.out.println("This is a gade checker program");
            System.out.println("Enter the marks from 0 to 100: ");
            System.out.println("Enter the marks: ");
            marks = scanner.nextInt();
            int grade = marks / 10;
            if (marks > 100) {
                System.out.println("Please enter the marks between the limit assigned");
            }
            else {
                switch(grade) {
                case 10:
                case 9:
                    System.out.println("Your grade is A");
                    break;
                case 8:
                case 7:
                    System.out.println("Your grade is B");
                    break;
                case 6:
                    System.out.println("Your grade is C");
                    break;
                case 5:
                case 4:
                    System.out.println("Your grade is D");
                    break;
                    
                default:
                    System.out.println("Your grade is E");
                    break;
                }
            }
            break;
        }
    }
}

You can just add a break;您可以添加一个break; statement just after all the operation is done inside the while loop block, see the above code block, this will end the loop when all your operations are done inside the loop.在while循环块内完成所有操作之后的语句,请参见上面的代码块,当您在循环内完成所有操作时,这将结束循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM