gpt4 book ai didi

java - 如何正确显示75分、30分、1分硬币的数量?

转载 作者:太空宇宙 更新时间:2023-11-04 10:38:49 24 4
gpt4 key购买 nike

我有一项任务,其中我必须创建一种方法,我必须从客户那里收到钱,并根据价格给他们找零。所需金额为 $130$55$25$5$1$0.75$0.30$0.01

目前令人沮丧的情况是,它无法正确显示硬币数量,但成功显示美元数量。例如,如果一件商品的价格为 $0.94,而有人给了我 $218,则每个金额的零钱应为 1(1x $130、1x $55、1x $25、1x $5 等)。但每当我询问这些值时,我都会得到两个 $1、零个 $0.75$0.30,以及六个 $0.01 硬币。

public class change {
static int ot;
static int numot;
static int ff;
static int numff;
static int tf;
static int numtf;
static int f;
static int numf;
static int o;
static int numo;
static int scc;
static double numscc;
static int tcc;
static double numtcc;
static int occ;
static double numocc;

public static double result(double salesTotal, double customerPayment){
System.out.println("How much does it cost?");
double price=IO.readDouble();
salesTotal=price;
System.out.println("How much do you have?");
double money=IO.readDouble();
customerPayment=money;
double change=money-price;

if (change>=0.01&&change/130>=1){
numot= (int) change/130;
change= change%130;
numff= (int) change/55;
change= change%55;
numtf= (int) change/25;
change= change%25;
numf= (int) change/5;
change= change%5;
numo= (int) change/1;
change= change%1;
numscc= change/0.75;
change= change%0.75;
numtcc= change/0.30;
change= change%0.30;
numocc= change/0.01;
change= change%0.01;
}
else if (change>=0.01&&change<130&&change/55>=1){
numff= (int) change/55;
change= change%55;
numtf= (int) change/25;
change= change%25;
numf= (int) change/5;
change= change%5;
numo= (int) change/1;
change= change%1;
numscc= change/0.75;
change= change%0.75;
numtcc= change/0.30;
change= change%0.30;
numocc= change/0.01;
change= change%0.01;
}
else if (change>=0.01&&change<55&&change/25>=1){
numtf= (int) change/25;
change= change%25;
numf= (int) change/5;
change= change%5;
numo= (int) change/1;
change= change%1;
numscc= change/0.75;
change= change%0.75;
numtcc= change/0.30;
change= change%0.30;
numocc= change/0.01;
change= change%0.01;
}
else if (change>=0.01&&change<25&&change/5>=1){
numf= (int) change/5;
change= change%5;
numo= (int) change/1;
change= change%1;
numscc= change/0.75;
change= change%0.75;
numtcc= change/0.30;
change= change%0.30;
numocc= change/0.01;
change= change%0.01;
}
return numff;
}

public static void main(String [] args){
double a= result(2,5);
IO.outputStringAnswer(numot + " $130 bills");
IO.outputStringAnswer(numff + " $55 bills");
IO.outputStringAnswer(numtf + " $25 bills");
IO.outputStringAnswer(numf + " $5 bills");
IO.outputStringAnswer(numo + " $1 bills");
IO.outputStringAnswer((int) numscc + " $0.75 coin");
IO.outputStringAnswer((int) numtcc + " $0.30 coin");
IO.outputStringAnswer((int) numocc + " $0.01 coin");
}
}

最佳答案

为了调试,我对你的程序进行了一些简化,但它的工作方式就像一个魅力:

    public class change {

static int ot, numot, ff, numff, tf, numtf, f, numf, o, numo, scc, nuscc, tcc, utcc, occ;
static double numscc, numtcc, numocc;

public static void main(String[] args) {
double change = 218 - 0.94;

if (change >= 0.01 && change / 130 >= 1) {
numot = (int) change / 130;
change %= 130;
numff = (int) change / 55;
change %= 55;
numtf = (int) change / 25;
change %= 25;
numf = (int) change / 5;
change %= 5;
numo = (int) change / 1;
change %= 1;
numscc = change / 0.75;
change %= 0.75;
numtcc = change / 0.30;
change %= 0.30;
numocc = change / 0.01;
} else {
System.err.println("Other value");
}

System.out.println(numot + " $130 bills");
System.out.println(numff + " $55 bills");
System.out.println(numtf + " $25 bills");
System.out.println(numf + " $5 bills");
System.out.println(numo + " $1 bills");
System.out.println((int) numscc + " $0.75 coin");
System.out.println((int) numtcc + " $0.30 coin");
System.out.println((int) numocc + " $0.01 coin");
}

}

所以,如果你遇到问题,我建议这是因为某种 IO 错误,当然不是因为你的逻辑。

关于java - 如何正确显示75分、30分、1分硬币的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49241155/

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