首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动售货机程序java

自动售货机程序java
EN

Stack Overflow用户
提问于 2016-05-12 10:17:42
回答 2查看 22.3K关注 0票数 3

在我的自动售货机代码中,如果我输入了多余的资金,它不会找回找给我的零钱。这里我漏掉了什么?

代码语言:javascript
复制
public static void main(String[] args) {
    int Food = runMenu();
    int Price = retrievePrice(Food);
    int change = moneyInserted(Price);
}

public static int runMenu(){
    Scanner keyboard = new Scanner(System.in);
    int choice = 0 ;
    System.out.println("\n\nPlease enter your selection:"
                + "\n1: Snickers \t 125"
                + "\n2: Reeses Cup \t 130"
                + "\n3: Doritoes \t 150"
                + "\n4: Pepsi \t 185"
                + "\n5: Exit ");
    choice = keyboard.nextInt();
    return choice;        
}

public static int retrievePrice(int menuChoice){
    if (menuChoice == 1)
        return 125;
    if (menuChoice == 2)
        return 130;
    if (menuChoice == 3)
        return 150;
    if (menuChoice == 4)
        return 185;
    else return 0;
}

public static int moneyInserted(int Price){
    Scanner keyboard = new Scanner(System.in);
    int money = 0;
    System.out.println("Your item costs: " + Price + " Please enter the amount of money:");
    money = keyboard.nextInt();
    while (money < Price){
        System.out.println("Please insert sufficient funds");
        money = money + keyboard.nextInt();
    }
    return money - Price ;
}

public static void changeOut(int change){
    int quarters = 0;
    int dimes = 0;
    int nickels = 0;
    while (change >= 25){
        quarters = quarters + 1;
        change = change - 25;
    }
    while (change >= 10){
        nickels = dimes + 1;
        change = change - 10;
        while (change >= 5){
            nickels = nickels + 1;
            change = change - 5;
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-05-12 11:44:17

您的代码确实返回了更改,只是您没有使用它。

像这样修改你的代码:

  1. 计算change后,将其传递给在代码中执行更改计算的changeOut()方法。为此,请将以下行添加到main()方法的末尾:

changeOut(change);

2.您的changeOut()方法中有一个小的逻辑错误。如下所示修改:

代码语言:javascript
复制
public static void changeOut(int change){
    int quarters = 0;
    int dimes = 0;
    int nickels = 0;
    while (change >= 25){
        quarters = quarters + 1;
        change = change - 25;
    }
    while (change >= 10){
        dimes = dimes + 1;
        change = change - 10;
    }
    while (change >= 5){
        nickels = nickels + 1;
        change = change - 5;
    }

    // to see the change, print it to the console perhaps
    System.out.printf("\nHere's your change:\n%d quarters, %d dimes, %d nickels and %d pennies!",
        quarters, dimes, nickels, change);
}
票数 2
EN

Stack Overflow用户

发布于 2019-12-27 20:00:31

代码语言:javascript
复制
private static int totalNotes = 0;
private static int amount = 0;

private static void printAmount(int maxAmt) {
    amount = amount - maxAmt;
    totalNotes++;
    System.out.print(maxAmt + "-");
}

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter Amount");
    amount = scanner.nextInt();
    int maxAmt = 0;

    while (amount != 0) {
        if (amount >= 2000) {
            maxAmt = 2000;
        } else if (amount >= 500) {
            maxAmt = 500;
        } else if (amount >= 200) {
            maxAmt = 200;
        } else if (amount >= 100) {
            maxAmt = 100;
        } else if (amount >= 50) {
            maxAmt = 50;
        } else if (amount >= 20) {
            maxAmt = 20;
        }
        // as same you can do this for
        /*10, 5, 2, 1*/
        printAmount(maxAmt);
    }
    System.out.println("\nTotal notes : " + totalNotes);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37176165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档