为什么输出结果与预期不符?

huangapple go评论76阅读模式
英文:

Why is the output not what is expected?

问题

以下是翻译好的部分:

这段代码的目标是计算商品的应付金额和实际支付金额然后找零出硬币和纸币的组合当我输入应付金额41.35实际支付金额50.00得到的结果是 8 美元2 个25美分硬币1个10美分硬币0个5美分硬币4个1美分硬币然而实际上结果应该是 8 美元2 个25美分硬币1个10美分硬币1个5美分硬币0个1美分硬币

import java.util.Scanner;
public class Cashier {
    private double amountDue;
    private double amountReceived;
    private int dollars = 0;
    private int quarters = 0;
    private int dimes = 0;
    private int nickels = 0;
    private int pennies = 0;
    private double toConvert;

    public Cashier(double due, double received){
        amountDue = due;
        amountReceived = received;
        toConvert = received - due;
    }
    public int getDollars(){
        dollars = (int)toConvert;
        toConvert -= dollars;
        return dollars;
    }
    public int getQuarters(){
        quarters = (int)(toConvert/.25);
        toConvert -= (.25 * quarters);
        return quarters;
    }
    public int getDimes(){
        dimes = (int)(toConvert/0.1);
        toConvert -= (.1 * dimes);
        return dimes;
    }
    public int getNickels(){
        nickels = (int)(toConvert/0.05);
        toConvert -= (0.05 * nickels);
        return nickels;
    }
    public int getPennies(){
        pennies = (int)(toConvert/0.01);
        toConvert -= (0.01 * pennies);
        return pennies;
    }
    public static void main(String[] args){
        Scanner ask = new Scanner(System.in);
        System.out.print("Enter Amount Due: ");
        double amtDue = ask.nextDouble();
        System.out.print("Enter Amount Received: ");
        double amtReceived = ask.nextDouble();

        Cashier one = new Cashier(amtDue, amtReceived);
        System.out.println("Dollars = " + one.getDollars());
        System.out.println("Quarters = " + one.getQuarters());
        System.out.println("Dimes = " + one.getDimes());
        System.out.println("Nickels = " + one.getNickels());
        System.out.println("Pennies = " + one.getPennies());

    }

}
英文:

This code is supposed to take the amount due for an item and the amount paid. Then find the change in coins and dollars. When ever I put due: 41.35 and received: 50.00, I get 8 dollars, 2 quarters, 1, dime, 0 nickels, 4 pennies. In reality, it should be 8 dollars, 2 quarters, 1, dime, 1 nickel, 0 pennies.

import java.util.Scanner;
public class Cashier {
private double amountDue;
private double amountReceived;
private int dollars = 0;
private int quarters = 0;
private int dimes = 0;
private int nickels = 0;
private int pennies = 0;
private double toConvert;
public Cashier(double due, double received){
amountDue = due;
amountReceived = received;
toConvert = received - due;
}
public int getDollars(){
dollars = (int)toConvert;
toConvert -= dollars;
return dollars;
}
public int getQuarters(){
quarters = (int)(toConvert/.25);
toConvert -= (.25 * quarters);
return quarters;
}
public int getDimes(){
dimes = (int)(toConvert/0.1);
toConvert -= (.1 * dimes);
return dimes;
}
public int getNickels(){
nickels = (int)(toConvert/0.05);
toConvert -= (0.05 * nickels);
return nickels;
}
public int getPennies(){
pennies = (int)(toConvert/0.01);
toConvert -= (0.01 * pennies);
return pennies;
}
public static void main(String[] args){
Scanner ask = new Scanner(System.in);
System.out.print("Enter Amount Due: ");
double amtDue = ask.nextDouble();
System.out.print("Enter Amount Received: ");
double amtReceived = ask.nextDouble();
Cashier one = new Cashier(amtDue, amtReceived);
System.out.println("Dollars = " + one.getDollars());
System.out.println("Quarters = " + one.getQuarters());
System.out.println("Dimes = " + one.getDimes());
System.out.println("Nickels = " + one.getNickels());
System.out.println("Pennies = " + one.getPennies());
}
}

答案1

得分: 1

在这里你遇到了一个有趣的问题。以下是我解决问题的方法。我首先尝试打印在每次运行函数后剩余的找零金额。我将主函数更改如下:

注意:我不得不将toConvert更改为static以在主函数中使用它

public static void main(String[] args) {
    Scanner ask = new Scanner(System.in);
    System.out.print("Enter Amount Due: ");
    double amtDue = ask.nextDouble();
    System.out.print("Enter Amount Received: ");
    double amtReceived = ask.nextDouble();

    TestA one = new TestA(amtDue, amtReceived);
    System.out.println("change due: " + toConvert);
    System.out.println("Dollars = " + one.getDollars());
    System.out.println("change due: " + toConvert);
    System.out.println("Quarters = " + one.getQuarters());
    System.out.println("change due: " + toConvert);
    System.out.println("Dimes = " + one.getDimes());
    System.out.println("change due: " + toConvert);
    System.out.println("Nickels = " + one.getNickels());
    System.out.println("change due: " + toConvert);
    System.out.println("Pennies = " + one.getPennies());
    System.out.println("change due: " + toConvert);
}

输出如下:

Enter Amount Due: 41.35
Enter Amount Received: 50.00
change: 8.649999999999999
Dollars = 8
change: 0.6499999999999986
Quarters = 2
change: 0.14999999999999858
Dimes = 1
change: 0.04999999999999857
Nickels = 0
change: 0.04999999999999857
Pennies = 4
change: 0.009999999999998573

这告诉我问题在于舍入double值。如果程序正常运行,第一部分的找零应该是8.65。这与double值在硬件上的存储方式有关,这是一个相当令人困惑的主题(至少对我来说如此)。

将值更改为float而不是double,程序就会正常工作。关于舍入double值,这个网站和其他网站上都有信息,有很多方法可以解决,比如格式化为固定小数位数并四舍五入等,但我认为将其更改为float是一个更简单的方法。

英文:

Interesting problem you ran into here. Here's how I went about figuring it out. First thing I did was try printing the amount of change left to break down after you ran each function. I changed the main function to look like this:

NOTE: I had to change toConvert to static to use it in the main function

public static void main(String[] args) {
Scanner ask = new Scanner(System.in);
System.out.print("Enter Amount Due: ");
double amtDue = ask.nextDouble();
System.out.print("Enter Amount Received: ");
double amtReceived = ask.nextDouble();
TestA one = new TestA(amtDue, amtReceived);
System.out.println("change due: " + toConvert);
System.out.println("Dollars = " + one.getDollars());
System.out.println("change due: " + toConvert);
System.out.println("Quarters = " + one.getQuarters());
System.out.println("change due: " + toConvert);
System.out.println("Dimes = " + one.getDimes());
System.out.println("change due: " + toConvert);
System.out.println("Nickels = " + one.getNickels());
System.out.println("change due: " + toConvert);
System.out.println("Pennies = " + one.getPennies());
System.out.println("change due: " + toConvert);
}

The output looked like this:

Enter Amount Due: 41.35
Enter Amount Received: 50.00
change: 8.649999999999999
Dollars = 8
change: 0.6499999999999986
Quarters = 2
change: 0.14999999999999858
Dimes = 1
change: 0.04999999999999857
Nickels = 0
change: 0.04999999999999857
Pennies = 4
change: 0.009999999999998573

This told me that the issue was in rounding your double values. The change should have been 8.65 in the first part if the program was running correctly. This has to do with how double values are stored on the hardware and its a pretty confusing topic(to me, at least).

Change the values to float instead of double and it'll work fine. There's info out there on this website and others about rounding double values and there are ways around it like formatting to x number of decimals and rounding and such but think changing it to float is an easier avenue.

答案2

得分: 0

你需要以美分为单位指定输入,这是您可以接受的最小货币形式。否则,对于70¢的费用,假设有3个25美分的硬币,您的代码将尝试将其转换为美元。

代码非常简单,可以编写为一个单独的递归函数,无需几十个特殊方法。我不会为您编写,但以下内容应该会给您一个很好的思路。

865 / 100 = 8
65 / 25 = 2
15 / 10 = 1
5 / 5 = 1
0 / 1 = 0
英文:

You need to designate the input in terms of cents, which is the smallest form of currency you can accept. Otherwise, for a charge of 70¢, given 3 quarters, your code would try to it convert it to dollars.

The code is very simple, and can be written as a single recursive function without the need for dozens of special methods. I won't do it for you, but the following should give you a very good idea.

865 / 100 = 8
65 / 25 = 2
15 / 10 = 1
5 / 5 = 1
0 / 1 = 0

答案3

得分: -1

在这个步骤之后:toConvert = received - due;
toConvert 金额变为 8.649999999999999
这需要额外的步骤将小数四舍五入到两位,使金额变为 8.65
将您的 Cashier 函数更改为以下内容:

public Cashier(double due, double received){
    amountDue = due;
    amountReceived = received;
    double num = received - due;
    toConvert = Math.round(num * 100.0) / 100.0;
    System.out.println(toConvert);
}

这个解决方案在使用 double 类型时是临时有效的。但是,将数字转换为分并使用算术运算是正确的做法。

英文:

After this step : toConvert = received - due;
The toConvert amount turns to 8.649999999999999
This requires an additional step to the round of decimal to two places so the amount turns 8.65
change your Cashier funtion to this:

public Cashier(double due, double received){
amountDue = due;
amountReceived = received;
double num = received - due;
toConvert = Math.round(num * 100.0) / 100.0;
System.out.println(toConvert);
}

This solution works temporary while working with a double. But converting figure to cents and using arithmetic solve is the right way.

huangapple
  • 本文由 发表于 2020年10月19日 09:58:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64420173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定