开发者社区> 问答> 正文

(JAVA)为我的自动贩卖机做while循环

这个项目我用带开关盒的do while循环来检查输入的盒是否匹配。我运行代码,但结果不是我想要的。我期望的是,如果用户键入错误的大小写,则do while循环将循环回到用户需要输入大小写的输入。

这是代码

package vending.machine;
    import java.util.Scanner;
    import java.util.*;
    import java.util.ArrayList;
    import static vending.machine.adddrinks.drinksList;

public class VendingMachine {
    public static void main (String []args){
        Scanner sc= new Scanner(System.in);

        double money;
        double total;
        double balance;

        do{
            System.out.println("\nPlease insert money:");
            money = sc.nextDouble();
            if(money < 1.2){
                System.out.println("Not enough money");
            }
        }while(money < 1.2);
        System.out.println("What drinks are you looking for");
        adddrinks.showDrinks();
        adddrinks.viewDrinks();

        System.out.print("Select: 1 or 2 or 3 or 4\n");
        int select=sc.nextInt();

        do{
            switch(select){
                case 1:{
                    total = adddrinks.drinksList.get(0).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }
                case 2:{
                    total = adddrinks.drinksList.get(1).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }
                case 3:{
                    total = adddrinks.drinksList.get(2).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }
                case 4:{
                    total = adddrinks.drinksList.get(3).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }

                default:{
                    System.out.println("Invalid");
                    break;
                }
            }

        }while(select<5);
    }
}

这是结果 在这里输入图像描述

问题来源:Stack Overflow

展开
收起
montos 2020-03-22 22:39:27 1596 0
1 条回答
写回答
取消 提交回答
  • 您需要遍历您的输入,我非常愿意改进您的代码(对不起,我不喜欢重复):

    private static void main10(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("What drinks are you looking for");
        adddrinks.showDrinks();
        adddrinks.viewDrinks();
        int select = 0;
        double balance = 0;
        boolean running = true;
        while (running) {
            if (sc.hasNextInt()) {
                select = sc.nextInt();
                if (0 < select && select <= adddrinks.drinksList.size()) {
                    double price = adddrinks.drinksList.get(select - 1).getdrinkPrice();
                    if (balance < price) {
                        System.out.println("Not enough money, " + select + " costs " + price);
                    } else {
                        balance -= price;
                        System.out.println("You choosed " + select + " , you will find it in the dispenser");
                    }
                } else {
                    System.out.println("Invalid input, please retry");
                }
            } else if (sc.hasNextDouble()) {
                balance += sc.nextDouble();
            } else {
                String input = sc.next();
                if (input == "q") {
                    running = false;
                    if (0 < balance)
                        System.out.println("please don't forget your change with amount of: " + balance);
                    System.out.println("Have a nice day, happy to see you again");
                    break;
                } else if (input == "h") {
                    System.out.println("What drinks are you looking for");
                    adddrinks.showDrinks();
                    adddrinks.viewDrinks();
                } else {
                    System.out.println("Invalid input, please retry");
                }
            }
            System.out.println("Your balance is: " + balance);
            System.out.println(
                    "please chouce your product (e.g 2), enter coins (e.g 2.0), click on 'h' to show product list or click on 'q' to get your change");
    
        }
    
    }
    

    回答来源:Stack Overflow

    2020-03-22 22:40:00
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载