【发布时间】:2016-05-12 02:17:41
【问题描述】:
在我的这个自动售货机代码中,如果我输入了多余的资金,它不会把零钱还给我。我在这里错过了什么?
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;
}
}
}
【问题讨论】:
-
你试过调试吗?
-
你在哪里调用changeOut?我没看到
-
我建议你应该正确地格式化你的代码。
-
Erm 我不熟悉季度和英镑,但我建议先将所有值转换为最低值。例如,在我的国家,我们有美元和美分。因此,要构建一个可以退货的自动售货机,我需要将输入的金额和以美分为单位的食物价格进行换算。然后 InputAmt - FoodPrice = ReturnMoney(都以美分为单位)
-
你计算了零钱的数量,但你没有打印出来。是的,这段代码未完成。是什么阻止您回馈零钱?
标签: java