gpt4 book ai didi

java - 另一个收银机程序

转载 作者:行者123 更新时间:2023-12-01 09:16:37 25 4
gpt4 key购买 nike

我想编写一个程序,该程序接受用户输入的价格、他们支付的金额以及找零的金额。我还希望程序指定应找零的硬币和纸币。

我认为我已经非常接近了,但是当我运行该程序时,我只得到 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 欧元 20 欧元 10 50℃0 20℃0 10c0 5c0 2c0 1c

正如您所看到的,它没有说明客户应该获得每张纸币/硬币的数量。

最佳答案

将这些代码移到后面

    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;

关于java - 另一个收银机程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40507702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com