【发布时间】:2021-05-09 08:04:54
【问题描述】:
我已经用 Java 编写了这个 switch case 程序。但是,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 函数在这个循环中不起作用..请帮帮我..
【问题讨论】:
-
你在
switch中有break,所以它只是从switch中分离出来,而不是从while中分离出来。您可以在switch语句的末尾再添加一个break语句。或者一个标签可以解决它。 -
您的中断都在交换机内部,因此它们只是从交换机中中断。在 switch 之后添加一个 break 以跳出循环。或者你可以只从方法中
return。 -
不是真正的重复,但 this other question 可能对“您可以从中
break的嵌套构造”的一般情况有所帮助。