public class Chair
{
private String typeChair;
private String materials;
private String color;
private double legs;
private double pricePerChair;
public void setTypeChair(String t) {
typeChair = t;
}
public String getTypeChair() {
return typeChair;
}
public void setMaterial(String m) {
materials = m;
}
public String getMaterials() {
return materials;
}
public void setColor(String c) {
color = c;
}
public String getColor() {
return color;
}
public void setLegs(double l) {
if (l >= 0 && l < 10) {
legs = l;
} else {
System.out.println("Legs must be between 0 and 9");
}
}
public double getLegs() {
return legs;
}
public void setPricePerChair(double p) {
if (p > 0) {
pricePerChair = p;
} else {
System.out.println("Price must be greater than 0");
}
}
public double getPricePerChair() {
return pricePerChair;
}
public double getCost() {
double cost = 0;
if (materials == "vinyl") {
cost += (pricePerChair * 0) + pricePerChair;
} else if (materials == "leather") {
cost += (pricePerChair * .4) + pricePerChair;
} else if (materials == "cloth") {
cost += (pricePerChair * .1) + pricePerChair;
}
return cost;
}
}
你好,我无法从材料中获得椅子的费用。材料是从用户输入的。如果材料是乙烯基,则附加成本为0%,皮革为0.4%,布为0.1%。每次我运行我的主方法,成本打印0.什么是代码中的问题?
答案 0 :(得分:1)
这里常见的错误 - 应始终将Java字符串与equals()
方法进行比较,而不是与==
进行比较。例如:
} else if (materials.equals("cloth")) {
答案 1 :(得分:0)
尝试.equals()而不是==进行比较