与えられたクラスのブール値を変更する方法が問題になっています。再び発生すると最後に値が設定されます。ブール値を格納する
public void purchase(Sandwich s) {
boolean owned = s.owned;
//I tried also with accessor and mutator here but then changed to public
String type = s.getType();
if (owned == false) {
if (money <= 0){
System.out.println("Worker " + this.name + " can not buy " + type + " sandwich, cuz he doesn't have enough money");
} else {
System.out.println("Worker " + this.name + " can buy " + type + " sandwich");
this.money = money;
owned = true;
//this is the place where it is supposed to change value to true (sandwich was bought and has owner now
s.owned = owned;
}
} else if (owned == true) {
System.out.println("Worker " + this.name + " can not buy " + type + " sandwich cuz it was bought");
System.out.println("Test");
}
}
問題は、与えられたサンドイッチが過去に購入したものの、その所有値は毎回falseに設定されていることである:これは私のクラス
public class Sandwich {
private String type;
private double price;
private String ing;
public boolean owned;
Sandwich (String t, double p, boolean o){
type = t;
price = p;
owned = o;
}
public boolean getO(){
return this.owned;
}
public void setO(boolean o){
this.owned = o;
}
public String getType(){
return this.type;
}
}
、それがアクセスして変更することになっている場所であります私はこのコードを実行しようとします。私はサンドイッチのために所有の変更された値を記録する必要がありますので、次回には所有する状態を実行する== true。方法
アプリケーションの実行の間にそれを維持する必要がありますか?または、アプリケーションが実行されているとき? – Joe
'(所有)'と '(所有している)'を使用してください。この場合、 '!owned'テストの反対のことは重複しています、'!owned'ではなく、 。 –
あなたはどんな問題を抱えていても、それは 'Sandwitch'クラスのブール値とは何の関係もありません。あなた自身の機能をテストする場合は、それを設定し、正しく読むことがわかります。投稿していないコードで何かしているのですが、所有しているのは「false」に設定されています –