我想写一个程序,它接受用户输入的价格,他们支付的金额以及他们的变化。我还希望程序指定硬币和纸币的更改。
我认为我非常接近,但是当我运行程序时,我只需要获得0金币和应付票据。
我不是在找人为我编写程序,我只需要知道我做错了什么,毫无疑问这是我忽略的一些简单但我无法理解的它出来了。
这是我到目前为止所做的事情,我知道它不是很漂亮,但随着我变得更有经验,我会继续努力。
感谢。
Scanner cashregister = new Scanner(System.in);
System.out.println("Thank you for your purchase!");
int amountDue = 0, amountPaid = 0, changeDue = 0;
int fiftyEuro = 5000;
int twentyEuro = 2000;
int tenner = 1000;
int fiver = 500;
int two = 200;
int one = 100;
int fiftyCent = 50;
int twentyCent = 20;
int tenCent = 10;
int fiveCent = 5;
int twoCent = 2;
int oneCent = 1;
fiftyEuro = changeDue / 5000;
changeDue = changeDue %= 5000;
twentyEuro = changeDue / 2000;
changeDue = changeDue %= 2000;
tenner = changeDue / 1000;
changeDue = changeDue %= 1000;
fiver = changeDue / 500;
changeDue = changeDue %= 500;
two = changeDue / 200;
changeDue = changeDue %= 200;
one = changeDue / 100;
changeDue = changeDue %= 100;
fiftyCent = changeDue / 50;
changeDue = changeDue %= 50;
twentyCent = changeDue / 20;
changeDue = changeDue %= 20;
tenCent = changeDue / 10;
changeDue = changeDue %= 10;
fiveCent = changeDue / 5;
changeDue = changeDue %= 5;
twoCent = changeDue / 2;
changeDue = changeDue %= 2;
oneCent = changeDue / 1;
changeDue = changeDue %= 1;
if (amountDue > amountPaid)
System.out.println("\nThe amount due must be less then or equal to the amount you are paying");
System.out.println("\nThe amount due, in cents: ");
amountDue = cashregister.nextInt();
System.out.println("\nThe amount paid, in cents: ");
amountPaid = cashregister.nextInt();
changeDue = amountPaid - amountDue;
System.out.println("\nYour change, in cents is: " + changeDue);
System.out.println("\nThe change for " + changeDue + " is: ");
System.out.println(fiftyEuro + " \u20ac" + "50"); //€50 notes due
System.out.println(twentyEuro + " \u20ac" + "20"); //€20 notes due
System.out.println(tenner + " \u20ac" + "10"); //€10 notes due
System.out.println(fiver + " \u20ac" + "5"); //€5 notes due
System.out.println(two + " \u20ac" + "2"); //€2 coins due
System.out.println(one + " \u20ac" + "1"); //€1 coins due
System.out.println(fiftyCent + " 50" + "c"); //50c coins due
System.out.println(twentyCent + " 20" + "c"); //20c coins due
System.out.println(tenCent + " 10" + "c"); //10c coins due
System.out.println(fiveCent + " 5" + "c"); //5c coins due
System.out.println(twoCent + " 2" + "c"); //2c coins due
System.out.println(oneCent + " 1" + "c"); //1c coins due
cashregister.close();
return;
}
}
以下是我输出的示例
感谢您的购买!
应付金额,以美分计: 2450
支付的金额,以美分为单位: 5000
您的更改,以美分为:2550
2550的变化是: 0€50 0€20 0€10 0€5 0€2 0€1 0 50c 0 20c 0 10c 0 5c 0 2c 0 1c
正如您所看到的那样,它并没有说出客户应该得到多少钞票/硬币。
答案 0 :(得分:1)
在
之后移动这些代码 fiftyEuro = changeDue / 5000;
changeDue = changeDue %= 5000;
twentyEuro = changeDue / 2000;
changeDue = changeDue %= 2000;
tenner = changeDue / 1000;
changeDue = changeDue %= 1000;
fiver = changeDue / 500;
changeDue = changeDue %= 500;
two = changeDue / 200;
changeDue = changeDue %= 200;
one = changeDue / 100;
changeDue = changeDue %= 100;
fiftyCent = changeDue / 50;
changeDue = changeDue %= 50;
twentyCent = changeDue / 20;
changeDue = changeDue %= 20;
tenCent = changeDue / 10;
changeDue = changeDue %= 10;
fiveCent = changeDue / 5;
changeDue = changeDue %= 5;
twoCent = changeDue / 2;
changeDue = changeDue %= 2;
oneCent = changeDue / 1;
changeDue = changeDue %= 1;
这些行之后..
System.out.println("\nThe amount paid, in cents: ");
amountPaid = cashregister.nextInt();
changeDue = amountPaid - amountDue;