可能重复:
将小数位移动到双精度
我正在创建装饰器模式的基本实现,使用冰淇淋和补充剂作为主题。一切正常,除了弹出一些奇怪的输出行为。
在看似随机的地方,成本将在点后面的第 16 个元素处四舍五入输出,这很奇怪,因为我使用的数字是 1.00 ( Vanilla
)、1.10 ( Chocolate
)、0.30 ( Sprinkles
)、0.40 ( WhippedCream
) 和 0.35 ( Nuts
)。
为了重新创建它,您需要以下类和接口:
public interface Icecream {
public double getCost();
public String getDescription();
}
public class Vanilla implements Icecream {
@Override
public double getCost() {
return 1.00;
}
@Override
public String getDescription() {
return "Vanilla ice";
}
}
public class Chocolate implements Icecream {
@Override
public double getCost() {
return 1.10;
}
@Override
public String getDescription() {
return "Chocolate ice";
}
}
public abstract class Decorator implements Icecream{
protected Icecream decoratedIcecream;
public Decorator(Icecream decoratedIcecream) {
this.decoratedIcecream = decoratedIcecream;
}
public abstract double getCost();
public abstract String getDescription();
}
public class Sprinkles extends Decorator {
public Sprinkles(Icecream decoratedIcecream) {
super(decoratedIcecream);
}
@Override
public double getCost() {
return decoratedIcecream.getCost() + 0.30;
}
@Override
public String getDescription() {
return decoratedIcecream.getDescription() + ", Sprinkles";
}
}
public class WhippedCream extends Decorator {
public WhippedCream(Icecream decoratedIcecream) {
super(decoratedIcecream);
}
@Override
public double getCost() {
return decoratedIcecream.getCost() + 0.40;
}
@Override
public String getDescription() {
return decoratedIcecream.getDescription() + ", Whipped cream";
}
}
public class Nuts extends Decorator {
public Nuts(Icecream decoratedIcecream) {
super(decoratedIcecream);
}
@Override
public double getCost() {
return decoratedIcecream.getCost() + 0.35;
}
@Override
public String getDescription() {
return decoratedIcecream.getDescription() + ", Nuts";
}
}
public class StartUp {
public static void main(String[] args) {
Icecream icecream = new Vanilla();
showDetails(icecream);
icecream = new Sprinkles(icecream);
showDetails(icecream);
icecream = new WhippedCream(icecream);
showDetails(icecream);
Icecream icecream2 = new Nuts(new Chocolate());
showDetails(icecream2);
icecream2 = new Sprinkles(icecream2);
showDetails(icecream2);
Icecream icecream3 = new Vanilla();
showDetails(icecream3);
icecream3 = new Nuts(icecream3);
showDetails(icecream3);
icecream3 = new Sprinkles(icecream3);
showDetails(icecream3);
}
private static void showDetails(Icecream icecream){
System.out.println("Cost: " + icecream.getCost() + "\t" + "Description: " + icecream.getDescription());
}
}
这导致以下输出:
Cost: 1.0 Description: Vanilla ice
Cost: 1.3 Description: Vanilla ice, Sprinkles
Cost: 1.7000000000000002 Description: Vanilla ice, Sprinkles, Whipped cream
Cost: 1.4500000000000002 Description: Chocolate ice, Nuts
Cost: 1.7500000000000002 Description: Chocolate ice, Nuts, Sprinkles
Cost: 1.0 Description: Vanilla ice
Cost: 1.35 Description: Vanilla ice, Nuts
Cost: 1.6500000000000001 Description: Vanilla ice, Nuts, Sprinkles
我不愿意添加这么多代码,但考虑到错误似乎是随机弹出的(当我添加生奶油时,当我创建巧克力 + 坚果时,当我添加洒水时两次)我真的无法确定我的哪些部分代码是相关的,哪些不是。
为什么它有时会舍入一个到处都声明为 double 并且点后面总是有 2 位数字的数字?