gpt4 book ai didi

java - -1 java中的计算错误

转载 作者:行者123 更新时间:2023-12-02 04:40:10 25 4
gpt4 key购买 nike

我从一位经验丰富的程序员那里得到了这个问题来准备编程测试。

Problem:

Given the amount of a purchase and the amount given as payment, calculate the number and type of each denomination of currency returned as change. Change is always given with the most of the highest denominations first. The denominations of paper currency used will be $100, $50, $20, $10, $5 and $1. The denominations of the coins will be $0.25, $0.10, $0.05 and $0.01.

INPUT: There will be 5 lines of input. Each line will contain 2 rational numbers representing the amount of a purchase and the amount given as payment.

OUTPUT: For each input line print the change using the number of non-zero denominations used in order of their magnitude from high to low. If no change is due print NONE. Sample Output #1 represents: 1-$5, 2-$1, 1-$.25, 2-$.10 and 2-$.01.

SAMPLE INPUT SAMPLE OUTPUT
1. 12.53, 20.00 1. 121221
2. 11.33, 15.00 2. 32112
3. 35.64, 50.00 3. 14111
4. 72.67, 100.00 4. 112113
5. 106.68, 200.00 5. 123112

我决定创建两个程序:一个较长、更容易理解的版本和一个精简版本。

这是长篇:

    import java.util.*;

public class Prac13CorrectChange {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

for (int A = 1; A <= 5; A++) {

System.out
.println("What is the amount of your purchase and the amount given as your payment?");
String input = in.nextLine();

String[] data = input.split(",");
double $amount = Double.parseDouble(data[0]);
double price = Double.parseDouble(data[1]);

double change = price - $amount;

int ans1 = ((int) (change / 100));
change = change - (ans1 * 100);

int ans2 = ((int) (change / 50));
change = change - (ans2 * 50);

int ans3 = ((int) (change / 20));
change = change - (ans3 * 20);

int ans4 = ((int) (change / 10));
change = change - (ans4 * 10);

int ans5 = ((int) (change / 5));
change = change - (ans5 * 5);

int ans6 = ((int) (change / 1));
change = change - (ans6 * 1);

int ans7 = ((int) (change / .25));
change = change - (ans7 * .25);

int ans8 = ((int) (change / .10));
change = change - (ans8 * .10);

int ans9 = ((int) (change / .05));
change = change - (ans9 * .05);

int ans10 = ((int) (change / .01));
change = change - (ans10 * .01);

String answer = "";

if (ans1 != 0) {

answer = answer + Integer.toString(ans1);

}

if (ans2 != 0) {

answer = answer + Integer.toString(ans2);

}

if (ans3 != 0) {

answer = answer + Integer.toString(ans3);

}

if (ans4 != 0) {

answer = answer + Integer.toString(ans4);

}

if (ans5 != 0) {

answer = answer + Integer.toString(ans5);

}

if (ans6 != 0) {

answer = answer + Integer.toString(ans6);

}

if (ans7 != 0) {

answer = answer + Integer.toString(ans7);

}

if (ans8 != 0) {

answer = answer + Integer.toString(ans8);

}

if (ans9 != 0) {

answer = answer + Integer.toString(ans9);

}

if (ans10 != 0) {

answer = answer + Integer.toString(ans10);

}

System.out.println(answer);

}
}

}

这是精简版。

    import java.util.Scanner;

public class Prac15CorrectChangeShort {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

for (int A = 1; A <= 5; A++) {

System.out
.println("What is the amount of your purchase and the amount given as your payment?");
String input = in.nextLine();

String[] data = input.split(",");
double $amount = Double.parseDouble(data[0]);
double price = Double.parseDouble(data[1]);

double change = price - $amount;
int ans[] = new int[10];
double money[] = { 100, 50, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01 };

for (int looptimes = 0; looptimes < 10; looptimes++) {

ans[looptimes] = ((int) (change / money[looptimes]));
change = change - (ans[looptimes] * money[looptimes]);

}

for (int looptimes = 0; looptimes < 10; looptimes++) {
if (ans[looptimes] != 0) {
System.out.print(ans[looptimes]);
}
}
System.out.print('\n');
}

}

}

我遇到的主要问题是,无论我进行什么调试,它都会给我错误的输出#2-5。最后一位数字少了 1。给我这个问题的程序员和我都找不到发生这种情况的任何原因。

请帮忙。

最佳答案

看起来像浮点错误。

Read this

计算变化时,您可以使用 BigDecimal 代替 double。

 BigDecimal change = BigDecimal.valueOf(price - $amount);

关于java - -1 java中的计算错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30281587/

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