简体   繁体   English

切换语句不会中断

[英]Switch statement won't break

I'm in the beginning of Java 7 Software design learning switch() and do...while () and what not. 我正在学习Java 7软件设计的开始switch()并做... while()而不是什么。

When I compile and run the program in Intelli J IDEA 13, it compiles and runs fine but nothing will break when the statements are complete. 当我在Intelli J IDEA 13中编译并运行该程序时,它可以编译并正常运行,但是在语句完成后不会中断任何操作。

For example, when the program is run, on case 2, when I enter -1 after I input 3 grades (90, 90, 90, -1) it gives me the average (270) but then it repeats... 例如,当程序在第2种情况下运行时,当我输入3个等级(90、90、90,-1)后输入-1时,它会给出平均值(270),但随后会重复...

It should break and go back to the beginning of the switch menu. 它应该会中断并返回到切换菜单的开头。 Even when I enter 3 at the menu, it just displays "No Code Here... " to infinity, even with a break statement (even though break statements are outdated). 即使我在菜单上输入3,即使使用break语句(即使break语句已过时),它也只会显示“ No Code Here ...”为无穷大。

import java.util.Scanner;
public class TestingPlatform {
public static void main(String[] args) {   

Scanner input = new Scanner(System.in);
    System.out.println("Enter 1 to set percentage of total for new grades, 2 to enter new      grades, 3 to get average, and 4 to quit: ");
    int choice = input.nextInt();

    do {
        switch (choice) {
            case 1:
                System.out.println("Enter percent to multiply by: ");
                double percent = input.nextDouble();
                System.out.println("You entered " + percent);
                break;
            case 2:
                int total;
                int gradeCounter;
                int grade;
                double average;
                total = 0;
                gradeCounter = 0;

                System.out.println("Enter grade or -1 to quit: ");
                grade = input.nextInt();

                while (grade != -1) {
                    total = total + grade;
                    gradeCounter = gradeCounter + 1;
                    System.out.println("Enter grade or -1 to quit: ");
                    grade = input.nextInt();
                }
                if (gradeCounter != 0) {
                    average = (double) total / gradeCounter;

                    System.out.printf("\nTotal of the %d grades is %d\n", gradeCounter, total);
                    System.out.printf("Class average is %.2f\n", average);
                } else
                    System.out.println("No grades were entered.");
                break;
            case 3:
                System.out.println("No code here yet....");
                break;

        }
    } while (choice != 4);
}
}

You are taking input once, then looping while input is not 4. Since the input never changes, that's forever. 您只获取一次输入,然后在输入未为4时循环。由于输入永不更改,因此将永远存在。

The break inside the switch just jumps out of the switch . break开关内刚跳出的switch Then you continue your loop. 然后,您继续循环。

Put these lines: 将这些行:

System.out.println("Enter 1 to set percentage of total for new grades, 2 to enter new      grades, 3 to get average, and 4 to quit: ");
choice = input.nextInt();

Inside the do loop, not outside it. do循环内部,而不是外部。 Then you will ask for new input every time you loop. 然后,您将在每次循环时请求新的输入。

You'll have to declare choice outside the loop though, since you reference it in the while at the bottom. 但是,您必须在循环外声明choice ,因为您在底部的while引用了它。

Put this line above the do loop: 将此行放在do循环上方:

int choice;

So the reason it will always loop again and hit the switch again is because you are never changing the value of choice again. 因此,它总是会再次循环并再次按下开关的原因是,您再也不会更改choice的值了。 The break statement is only breaking out of the switch statement, not the loop itself. break语句仅脱离switch语句,而不是循环本身。 After you finish the switch you need to ask the user again if they want to pick another option or if they want to enter four to quit. 完成switch您需要再次询问用户是否要选择其他选项,或者是否要输入四个退出选项。

break statements will only break out of the inner most...group? break语句只会跳出最里面的...组? Statement? 声明? I'm not sure what the correct word would be, but it only will break out one level, not all the way. 我不知道正确的词是什么,但是它只会分解出一个层次,而不是全部。

Just for the sake of saying it, always remember that you have four parts of a loop: Initialization, Condition, Body, and Changer. 只是为了说一下,请始终记住循环包含四个部分:初始化,条件,主体和转换器。

Initialization: where the variables IN THE CONDITION are assigned their initial values; 初始化:为条件中的变量分配初始值; this could happen inside the loop in the case of a do...while(); do...while();的情况下,这可能会在循环内发生do...while(); and there could be multiple locations 而且可能有多个位置

Condition: the part that decides if your loop will continue or not 条件:决定循环是否继续的部分

Body: the code that is being looped over and over 正文:不断循环的代码

Changer: (this is the one most often missed, I've noticed) a spot, or spots, where variables IN YOUR CONDITION are having their values changed; 换器:(这是我注意到的最常错过的地方)一个或多个斑点,您的状态中的变量的值正在更改; there could be multiple locations and multiple variables being affected 可能有多个位置和多个变量受到影响

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

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