-1 java中的计算错误

时间:2015-05-16 22:59:05

标签: java output currency money-format

我从一位经验丰富的程序员那里解决了这个问题,为编程测试做准备。

  

问题:

     

根据购买金额和付款金额,计算作为变更返回的每种货币面额的数量和类型。总是以最高面额的大多数给出变化。使用纸币的面额将分别为100美元,50美元,20美元,10美元,5美元和1美元。硬币的面额将分别为0.25美元,0.10美元,0.05美元和0.01美元。

     

INPUT:将有5行输入。每行将包含2个有理数字,表示购买金额和付款金额。

     

输出:对于每个输入行,使用按其数量级从高到低的顺序使用的非零面额数打印更改。如果没有更改,则打印NONE。样本输出#1代表:1- $ 5,2- $ 1,1- $ 25,2- $ 10和2 - $。01。

     

示例输入样本输出
  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。既没有给我这个问题的程序员,也没有找到任何理由。

请帮忙。

1 个答案:

答案 0 :(得分:2)

看起来像是浮点错误。

Read this

在计算更改时,您可以使用BigDecimal而不是double。

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