Java - 程序没有突破循环?

时间:2015-08-11 12:06:54

标签: java while-loop

while (choice != 7) { 

    System.out.println("---  Mathematical Calculator  ---");

    System.out.println("");

    System.out.println("Pick an operation from the list - Use nos. 1 to 7");
    System.out.println("1) Multiplication");
    System.out.println("2) Division");
    System.out.println("3) Addition");
    System.out.println("4) Subtraction");
    System.out.println("5) Find the area of a regular object");
    System.out.println("6) Find the volume of a regular object");
    System.out.println("7) Exit");

    choice = userInput.nextInt(); 

    switch (choice) {

    case 1: {
        System.out.println("");
        System.out.println("You have chosen multiplication");
        System.out.println("Enter a number");
        double num1 = userInput.nextDouble();
        System.out.println("Enter another number");
        double num2 = userInput.nextDouble();

        double num3 = num1 * num2;

        System.out.println(num1 + " * " + num2 + " = " + num3);         
        num1 = num3;

        while (choice2 != 5 || choice2 != 6) {

            System.out.println(""); System.out.println("");
            System.out.println("If you would like to build on the answer obtained - pick an operation from the list - Use nos. 1 to 4");
            System.out.println("Else press 5 to return to the main menu");
            System.out.println("1) Multiplication");
            System.out.println("2) Division");
            System.out.println("3) Addition");
            System.out.println("4) Subtraction");
            System.out.println("5) Start new calculation");
            System.out.println("6) Exit");

            choice2 = userInput.nextInt();

            switch (choice2) {

            case 1: 
            {
                System.out.println("Enter number");
                num2 = userInput.nextDouble();
                num3 = num1 * num2;
                System.out.println(num1 + " * " + num2 + " = " + num3);
                num1 = num3;
                break;
            }

            case 2: 
            {
                System.out.println("Enter number");
                num2 = userInput.nextDouble();
                num3 = num1 / num2;
                System.out.println(num1 + " / " + num2 + " = " + num3);
                num1 = num3;
                reak;
            }

            case 3: 
            {
                System.out.println("Enter number");
                num2 = userInput.nextDouble();
                num3 = num1 + num2;
                System.out.println(num1 + " + " + num2 + " = " + num3);
                num1 = num3;
                break;
            }

            case 4: 
            {
                System.out.println("Enter number");
                num2 = userInput.nextDouble();
                num3 = num1 - num2;
                System.out.println(num1 + " - " + num2 + " = " + num3);
                num1 = num3;
                break;
            }

            case 5: choice = 0; break;

            case 6: choice = 7; break;

            default: System.out.println("Invalid choice");
        }
        choice2 = 0;
    }
    break;
}

我发布了一小段代码。我的问题是,当我在第二个查询(choice2)中输入5或6时,我的程序继续循环而不是退出循环并返回主菜单/终止程序。

希望得到一些关于我做错事的反馈。

2 个答案:

答案 0 :(得分:6)

你的while (choice2 != 5 || choice2 != 6)错了,因为它总是会导致真的。如果choice2为5,则第二个子句为真。

更不用说你总是在循环结束时将choice2设置为0,所以即使你用||替换&&,循环也会永远继续。

答案 1 :(得分:3)

条件choice2 != 5 || choice2 != 6始终为真,因为没有数字同时等于5和6。

如果您想在输入5或6时退出循环,请改用&&

while (choice2 != 5 && choice2 != 6)

确保在继续循环的下一次迭代之前设置choice2

请注意,您可以使用带标签的break结构从switch语句中突破循环:

calc: // Label the loop for using labeled break
while (true) {
    ...
    switch(...) {
        case 1:
            break;          // This will exit the switch
        ...
        case 6: break calc; // This will exit the loop
    }
}