试图了解我的循环哪里出了问题

Trying to Understand Where My Loop Goes Wrong

所以这是我的任务:

A postal company for a package charges for the first pound or a fraction thereof and per pound for anything over one pound. Write a program that prints the charge of a package.

Variables:

weight

First execution:

Weight? -12 Weight must be a positive number.

Second Execution:

Weight? 0 Weight must be a positive number.

Third Execution:

Weight? 2 Pay: .00

Forth Execution:

Weight? 2.8 Pay: .00

Fifth Execution:

Weight? 2.07 Pay: .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 
        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("[=10=].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("[=10=].00");
            System.out.println("Pay: " + money.format(output));

        }
    }
}

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

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

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

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

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

如果我对这个问题的理解正确,你不应该有任何小数美元金额,因为超过一磅的任何东西都会自动四舍五入到下一磅。即: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 
    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("[=10=].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("[=10=].00");
        System.out.println("Pay: " + money.format(output));

    }
}

}

这样一来,您就不必费心增加 10 美分了。我希望这有帮助。如果您有任何问题,请告诉我。

这是我想出的:

    Scanner keyboard = new Scanner(System.in);
    double weight;
    double cost = 15.00; // set first pound to 
    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("[=10=].00");
        System.out.println("Pay: " + money.format(output));
    }

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