簡體   English   中英

當滿足while條件時,為什么第一次do while循環沒有結束?

[英]Why doesn't the first do while loop end when the while condition is met?

我遇到的主要問題是在第一次執行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!");
    }
}

重新閱讀您的問題后,我終於了解了您的真正需求。

實際上,如果要在不滿足第一個循環的條件時退出,則由於您具有嵌套循環,因此必須更改內部循環的條件。

另外,您需要將內部循環從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,則使用此算法將直接退出。

如果將while條件更改為usersMoney == -1則它應該可以正常工作。 正如您所寫的,您是在要求用戶輸入-1退出,盡管在您的情況下您會告訴“僅當usersMoney與-1不同時”。 我還建議在switch內部使用default而不是冗長的if 只需將其替換為: default: System.out.print("Invalid Entry!\\t");

不需要嵌套循環。 當滿足內部循環的條件時,第一個條件可能沒有,因此它將繼續運行,並且除非用戶也選擇“ X”,否則當用戶使用-1時它將永遠不會退出。

嘗試這個

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!");

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM