I'm in the beginning of Java 7 Software design learning switch() and do...while () and what not.

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.

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...

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).

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.

The break inside the switch just jumps out of the 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. 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.

Put this line above the do loop:

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. The break statement is only breaking out of the switch statement, not the loop itself. 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.

break statements will only break out of the inner most...group? 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(); 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归因
不隶属于 StackOverflow
scroll top