0

所以这是我的任务:

一个包裹的邮政公司对第一磅或其一小部分收取 15 美元,对于超过一磅的任何东西收取每磅 10 美元。编写一个打印包裹费用的程序。

变量:

重量

第一次执行:

重量?-12 权重必须是正数。

第二次执行:

重量?0 权重必须为正数。

第三次执行:

重量?2 支付:25.00 美元

第四次执行:

重量?2.8 支付:33.00 美元

第五次执行:

重量?2.07 支付:25.70 美元

这是我到目前为止开发的代码:

 import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        double weight;
        double cost = 15.00; // set first pound to $15
        double output = 0;
        System.out.print("Weight?: ");
        weight = keyboard.nextDouble();
        if (weight <= 0) {
            System.out.println("Weight must be a positive number.");
        } else if (weight == 1) {
            // Print the charge of the package
            output = output + cost;
            DecimalFormat money = new DecimalFormat("$0.00");
            System.out.println("Pay: " + money.format(output));
        } else {
            for (double i = 1; i < weight; i = i + .01) {
                if (weight > 1) {
                    output = output + (1 / 10.00);
                }
            }
            // Print the charge of the package
            output = output + cost;
            DecimalFormat money = new DecimalFormat("$0.00");
            System.out.println("Pay: " + money.format(output));

        }
    }
}

一切正常,但我不明白为什么(特别是在第四次和第五次执行中)最终输出总是 0.10 美分。谁能帮我达到我需要的准确性?

4

3 回答 3

0

这:double i = 1; i < weight; i = i + .01可能是你的问题。

双精度数对于十进制数学并不精确。您期望 i == 重量,此时循环应该停止,但这可能不是因为 i + .01(无论多少次)比重量小一点。

我的建议是放弃循环。如果包裹超过 1 磅,只需从重量中减去 1 磅,乘以每磅 10 美元,然后四舍五入到您需要的小数点后两位(注意:根据指定的四舍五入方式四舍五入,不要'不要只是让从双精度到十进制的转换自己完成。有多种方法可以四舍五入,而十进制并不神奇地知道哪一种适合您的问题。)

编辑:看看你的解决方案,它应该只适用于 1/10 磅的分辨率吗?如果是这样,从四舍五入开始。同样,根据需要舍入的方式(向下、向上或最近)对其进行四舍五入。

于 2015-09-15T20:13:03.753 回答
0

如果我正确理解了这个问题,那么您永远不应该有任何小数美元金额,因为超过一磅的任何东西都会自动四舍五入到下一磅。即:2.01 磅将变为 3 磅。如果这是正确的,那么您可以使用 Math 的 ceil 函数将重量四舍五入到最接近的整磅,然后执行以下操作:

public class Main {
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    double weight;
    double cost = 15.00; // set first pound to $15
    double output = 0;
    System.out.print("Weight?: ");
    weight = keyboard.nextDouble();
    if (weight <= 0) {
        System.out.println("Weight must be a positive number.");
    } else if (weight == 1) {
        // Print the charge of the package
        output = output + cost;
        DecimalFormat money = new DecimalFormat("$0.00");
        System.out.println("Pay: " + money.format(output));
    } else {
        double temp = (Math.ceil(weight)) - 1;
        for(double i = temp; i > 0; i-- ) {
            output += 10;
        }
        output += cost;
        DecimalFormat money = new DecimalFormat("$0.00");
        System.out.println("Pay: " + money.format(output));

    }
}

}

这样,您就无需担心 10 美分的增量。我希望这有帮助。如果您有任何问题,请告诉我。

于 2015-09-15T20:17:18.823 回答
0

这是我想出的:

    Scanner keyboard = new Scanner(System.in);
    double weight;
    double cost = 15.00; // set first pound to $15
    double output = 0;
    System.out.print("Weight?: ");
    weight = keyboard.nextDouble();
    if (weight <= 0) {
        System.out.println("Weight must be a positive number.");
    } else {
        // Print the charge of the package
        if (weight > 1) {
            output = cost + ((weight-1) * 10);
        } else {
            output = cost;
        }
        DecimalFormat money = new DecimalFormat("$0.00");
        System.out.println("Pay: " + money.format(output));
    }

这应该可以处理您的所有情况,以及 0 到 1 之间的数字,假设它是每 0.1 磅 1 美元。而不是你的for循环,你可以只使用cost + ((weight-1) * 10)公式。我删除了检查权重是否等于 1,因为它在 else 子句中处理。

于 2015-09-15T20:22:45.120 回答