-2

私はFoodという抽象クラスを持っており、そこでdouble currentCashを59に初期化しています。Foodのサブクラスが2つあります。果物と肉では、59 の現金準備金からユーザーが入力した価格を差し引きます。ユーザーが価格を入力した後、果物または肉の種類に関係なく、次の食品オブジェクトに進みます。現金準備金は再び 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 に戻ります。

はいと入力しました

どんなリンゴ

マック

優先順位を入力してください

1

支払いたい価格を入力

1

りんごの種類:マック

Apple の支払い希望価格: 1.0 ドル。

リストには 58.0 ドルが残っています。

4

2 に答える 2

2

メソッドcashReserves()が の値を変更することはありませんcurrentCash

//currentCash=newCash;の値を変更するコメント アウトされた行がありますがcurrentCash、... コメント アウトされています。


編集:問題の行のコメントを外してブロックから移動した元の質問に対する現在の編集を考えると、変数の値が適切に設定されていないelseと推測できます。pricenewCashに設定しても構いません。priceの場合0、次の行:

newCash = currentCash - price;

と同じです

newCash = currentCash - 0;

あるいは単に

newCash = currentCash;

したがって、メソッドの後半で次のことを行います。

currentCash = newCash;

そして、変更されていることを期待currentCashして(そしてそれが「リセット」されていると主張しています)、実際の問題は、単に の値を変更していないことですcurrentCashnewCashこのメソッドを呼び出す前に何があったかは問題ではありません。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

おそらく、別のクラスでユーザー入力を取得しており、それを使用して set を使用する必要がある場合がありますprice。この場合、setterforを作成する必要があります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
}
于 2013-10-30T15:30:52.307 に答える
1

どのように価格を入力として取っていますか?

また

このようなコードを試すこともできます

currentCash= currentCash-price;

差分値を格納するために新しい変数を定義する必要はありません。

于 2013-10-30T16:26:27.300 に答える