【发布时间】:2016-12-21 13:03:55
【问题描述】:
我想在我的 ToString 中显示一个方法的结果,我该怎么做? 到目前为止,这是我的代码: 你可以在底线看到我不知道要写什么来获得“updatedPrice”的结果,你能帮忙吗?
public double updatedPrice(double price){
this.price=price;
double ChangePriceRate, ChangePriceAmount, finalPrice;
if(name=="Bamba"){
ChangePriceRate = 0.15;
}else{
ChangePriceRate = 0.05;
}
ChangePriceAmount = price * ChangePriceRate;
if(name=="Bamba"){
finalPrice = price + ChangePriceAmount;
}else{
finalPrice = price - ChangePriceAmount;
}
}
public String toString (){
return "Name of the Snack: "+name+ "\n"+
"Name of the Company: "+comp+ "\n"+
"Price before discount: "+this.price+ "\n"+
"Price after discount: "+ **finalPrice?** + "\n";
}
b.t.w - 我真的是新手,完全是初学者。** 谢谢。
【问题讨论】:
-
您的代码将无法编译。方法
updatedPrice(double price)中没有return语句 -
1) 使用 "return finalPrice;"在您的 updatePrice 方法中。在 toString() 调用 updatePrice 方法 2) 将 updatePrice 更改为 void 并使 finalPrice 成为实例变量而不是方法本地