我有一个名为Food的抽象类,我将double currentCash初始化为59.我有2个子类的Food;水果和肉类,我从我的现金储备59中减去用户输入的价格。用户输入价格后,然后进入下一个Food对象,无论是Fruit还是Meat类型,现金储备再次返回59
abstract public class Food implements Interface {//abstract class.
static public double currentCash=59;
double newCash=0;
}
public class Fruit extends Food implements Interface {
public String name;
public double price;
public String setName;
public int priority;
public Fruit() {
name="no name yet.";
price=0;
priority=0;
realPrice=0;
}
public Fruit(String initialName, int initialPriority, double initalPrice, double initalrealPrice){
name=initialName;
priority=initialPriority;
price=initalPrice;
realPrice=initalrealPrice;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
if (priority > 0 && priority <= 7) {
this.priority = priority;
} else {
System.err.println("Error, enter 1 through 7");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setrealPrice(double realPrice){
this.realPrice=realPrice;
}
@Override
public void writeOutput(){
System.out.println ("Name: "+getName()+" Priority: "+priority +" Price: "+price );
}
public boolean hasSameName (Fruit otherFruit){
return this.name.equalsIgnoreCase(otherFruit.name);
}
public void setPrice(double price) {
this.price=price;
}
public double getPrice(){
return price;
}
public double realPrice(){
return realPrice;
}
@Override
public String eatable() {
String eatablePrint= "Interface eatable";
return eatablePrint;
}
@Override
public void cashReserves() {
newCash= currentCash-price;
if (newCash>0){
System.out.println("You have "+newCash+" dollars left for your list.\n");
}
else
{
String k = "out of cash";
System.out.println(k);
}
currentCash=newCash;
}
@Override
public void realPrices() {
double realCash;
realCash=currentCash-realPrice;// the price you want to pay is what you type in, but the actual price is here.
System.out.println("The grocery store price is " +realPrice+" dollars, and you have "+ realCash+" dollars left.\n");
if (realCash<10){
System.out.println("You're running out of real cash");
}
if (realCash<=0){
System.out.println("You're out of real cash");
}
}
}
在每个cashReserves方法之后,currentCash再次返回59,而不是用户输入价格后的新值。
您输入了是
什么样的苹果
MAC
输入优先级
1
输入您要支付的价格
1
苹果类型:mac
Apple要支付的价格:1.0美元。
您的清单还有58.0美元。
答案 0 :(得分:2)
您的方法cashReserves()
永远不会修改currentCash
。
你有一个注释掉的行//currentCash=newCash;
,它会修改currentCash
的值,但是......它被注释掉了。
编辑:鉴于对原始问题的当前修改,您已在其中取消注释相关行并将其移出else
块,我只能猜测您{{1}的价值}变量未正确设置。 price
设置为什么并不重要。如果newCash
为price
,则为以下行:
0
与
相同newCash = currentCash - price;
或只是
newCash = currentCash - 0;
稍后在您的方法中执行:
newCash = currentCash;
并且期望currentCash = newCash;
已经改变(并声称它正在“重置”),您的问题实际上是您根本不会更改currentCash
的值。在调用此方法之前,currentCash
是什么并不重要。如果newCash
为price
,则您的方法设置为0
,然后设置newCash = currentCash
...这就像说currentCash = newCash
一样,所以只留下currentCash = currentCash
59
。
EDIT2:对答案的另一个编辑......我对edit1的怀疑是完全正确的。你有:
price=0;
在你的构造函数中。
newCash = currentCash /*59*/ - price /*0*/;
//newCash = 59
currentCash = newCash /*59*/;
//currentCash = 59
什么都没有重置。你永远不会改变currentCash
的价值。
也许您正在另一个类中接受用户输入,并且需要使用它来设置price
。在这种情况下,您需要为setter
:
price
public void setPrice(double newPrice) {
price = newPrice;
}
或者让cashReserves
设置price
(前者会优于后者,我认为)。
public void cashReserves(double newPrice) {
price = newPrice;
//do everything else your cashReserves method is currently doing
}
答案 1 :(得分:1)
您如何将价格作为投入?
或
您也可以尝试这样的代码
currentCash= currentCash-price;
无需定义新变量来存储差值。