当满足while条件时,为什么第一个while循环结束?

时间:2015-02-20 02:50:43

标签: java do-while

我遇到的主要问题是第一次执行while循环,当条件满足时我无法退出程序。

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    final double milkPrice = 2;
    final double sodaPrice = 2.25;
    final double cbarPrice = 1.25;
    final double gbearsPrice = 1.50;
    final double chipsPrice = 1;
    double usersMoney = 0;
    double moneyLeft = 0;
    double tempMoneyLeft = 0;
    int milkQnty = 5;
    int sodaQnty = 4;
    int cbarQnty = 5;
    int gbearsQnty = 6;
    int chipsQnty = 6;
    char usersSelection;

    do{
        System.out.println("Welcome to the Vending Machine!");
        System.out.println("I sense that your are hungry... This is what I have to offer:");
        System.out.println();
        System.out.println("\tItem" + "\t\tPrice" + "\t\tQuantity");
        System.out.println("\tMilk" + "\t\t$2.00" + "\t\t" + milkQnty);
        System.out.println("\tSoda" + "\t\t$2.25" + "\t\t" + sodaQnty);
        System.out.println("\tCandy Bar" + "\t$1.25" + "\t\t" + cbarQnty);
        System.out.println("\tGummy Bears" + "\t$1.50" + "\t\t" + gbearsQnty);
        System.out.println("\tChips" + "\t\t$1.00" + "\t\t" + chipsQnty);
        System.out.println();
        System.out.print("Please enter how much money you have to spend (enter -1 to shut down): ");
        usersMoney = scan.nextDouble(); 

具体而言,我认为这就是我遇到问题的地方。它只是继续进行下一个恰好是嵌套的do-while循环的语句。

            do {
                System.out.print("Please make a selection:" + 
                        "\nA-Milk, B-Soda, C-Candy Bar, D-Gummy Bears, E-Chips, X-Exit: ");
                usersSelection = scan.next().toUpperCase().charAt(0);

                switch (usersSelection) {
                case 'A':
                    tempMoneyLeft = usersMoney - milkPrice;
                    moneyLeft = tempMoneyLeft - moneyLeft;
                    System.out.println("You have bought Milk for " + milkPrice + ". You still have " +
                            moneyLeft + " left.");
                    System.out.println();
                    break;

                case 'B':
                    tempMoneyLeft = usersMoney - sodaPrice;
                    moneyLeft = tempMoneyLeft - moneyLeft;
                    System.out.println("You have bought Soda for " + sodaPrice + ". You still have " +
                            moneyLeft + " left.");
                    System.out.println();
                    break;

                case 'C':
                    tempMoneyLeft = usersMoney - cbarPrice;
                    moneyLeft = tempMoneyLeft - moneyLeft;
                    System.out.println("You have bought Candy Bar for " + cbarPrice + ". You still have " +
                            moneyLeft + " left.");
                    System.out.println();
                    break;

                case 'D':
                    tempMoneyLeft = usersMoney - gbearsPrice;
                    moneyLeft = tempMoneyLeft - moneyLeft;
                    System.out.println("You have bought Gummy Bears for " + gbearsPrice + ". You still have " +
                            moneyLeft + " left.");
                    System.out.println();
                    break;

                case 'E':
                    tempMoneyLeft = usersMoney - chipsPrice;
                    moneyLeft = tempMoneyLeft - moneyLeft;
                    System.out.println("You have bought Chips for " + chipsPrice + ". You still have " +
                            moneyLeft + " left.");
                    System.out.println();
                    break;

                case 'X':
                    System.out.println("Thank you for your purchases: Your change is " + moneyLeft);
                    break;
                }
                if(!(usersSelection == 'A' || usersSelection == 'B' || usersSelection == 'C' || usersSelection == 'D'
                        || usersSelection == 'E' || usersSelection == 'X')) {
                    System.out.print("Invalid Entry!\t");
                }
            } while (usersSelection != 'X');

        } while (usersMoney != -1);

        System.out.println();
        System.out.print("Thank you for your business!");
    }
}

3 个答案:

答案 0 :(得分:1)

重新阅读你的问题之后,我终于理解了你真正想要的东西。

实际上,如果你想在不满足第一个循环的条件时退出,那么由于你有嵌套循环,你必须改变内部的条件。

此外,您需要将内部循环从do..while更改为简单的while。

所以你的代码看起来像这样:

char usersSelection = ' ';

do {
    usersMoney = scan.nextDouble();
    while(usersMoney != -1 && usersSelection != 'X')//will be automatically skipped if -1 is entered
    {
        System.out.print("Please make a selection:"
                + "\nA-Milk, B-Soda, C-Candy Bar, D-Gummy Bears, E-Chips, X-Exit: ");
        usersSelection = scan.next().toUpperCase().charAt(0);

        //switch statement here
    }

} while (usersMoney != -1);

如果输入-1,则使用此算法将直接退出。

答案 1 :(得分:0)

如果您将while条件更改为usersMoney == -1,它应该可以正常工作。正如你所写的,你要求用户输入-1退出,尽管在你的情况下你只是告诉“只有当usersMoney不同于-1”时。 我还建议在default内使用switch而不是冗长的if。只需将其替换为: default: System.out.print("Invalid Entry!\t");

答案 2 :(得分:0)

不需要嵌套循环。当满足内循环的条件时,第一个循环的条件可能没有,因此它将继续运行,并且当用户具有-1时它将永远不会退出,除非他也选择' X'。

试试这个

do {
    System.out.print("Please make a selection:" + 
            "\nA-Milk, B-Soda, C-Candy Bar, D-Gummy Bears, E-Chips, X-Exit: ");
    usersSelection = scan.next().toUpperCase().charAt(0);

    switch (usersSelection) {
    case 'A':
        tempMoneyLeft = usersMoney - milkPrice;
        moneyLeft = tempMoneyLeft - moneyLeft;
        System.out.println("You have bought Milk for " + milkPrice + ". You still have " +
                moneyLeft + " left.");
        System.out.println();
        break;

    case 'B':
        tempMoneyLeft = usersMoney - sodaPrice;
        moneyLeft = tempMoneyLeft - moneyLeft;
        System.out.println("You have bought Soda for " + sodaPrice + ". You still have " +
                moneyLeft + " left.");
        System.out.println();
        break;

    case 'C':
        tempMoneyLeft = usersMoney - cbarPrice;
        moneyLeft = tempMoneyLeft - moneyLeft;
        System.out.println("You have bought Candy Bar for " + cbarPrice + ". You still have " +
                moneyLeft + " left.");
        System.out.println();
        break;

    case 'D':
        tempMoneyLeft = usersMoney - gbearsPrice;
        moneyLeft = tempMoneyLeft - moneyLeft;
        System.out.println("You have bought Gummy Bears for " + gbearsPrice + ". You still have " +
                moneyLeft + " left.");
        System.out.println();
        break;

    case 'E':
        tempMoneyLeft = usersMoney - chipsPrice;
        moneyLeft = tempMoneyLeft - moneyLeft;
        System.out.println("You have bought Chips for " + chipsPrice + ". You still have " +
                moneyLeft + " left.");
        System.out.println();
        break;

    case 'X':
        System.out.println("Thank you for your purchases: Your change is " + moneyLeft);
        break;
    }
    if(!(usersSelection == 'A' || usersSelection == 'B' || usersSelection == 'C' || usersSelection == 'D'
            || usersSelection == 'E' || usersSelection == 'X')) {
        System.out.print("Invalid Entry!\t");
    }
    if(usersMoney < 0) break;

} while (usersSelection != 'X');

System.out.println();
System.out.print("Thank you for your business!");

}

相关问题