私はリンゴのタイプが「赤味」、「ゴールデンデリシャス」、「ガラ」、「おばあちゃんスミス」のみであるという特定のパラメータを持つリンゴプログラムを作成することが任されています。Java:文字列メソッドが正しく変更されていませんか?
しかし、何らかの理由で私がクラスに電話しても、「おばあちゃんスミス」にリンゴタイプを設定しても、「これは無効なタイプのリンゴです。また、 "Gala"のデフォルトのタイプ名は変更されません。たぶん私のif
ステートメントが間違っていますか?
ここでアップルのクラスがあります:
public class Apple {
private String type;
private double weight;
private double price;
//Default apple values (Constructors)
public Apple()
{
this.type = "Gala";
this.weight = 0.5;
this.price = 0.89;
}
//Accessors
public String getType()
{
return this.type;
}
public double getWeight()
{
return this.weight;
}
public double getPrice()
{
return this.price;
}
//Mutators
public void setType (String aType)
{
if (!aType.equalsIgnoreCase("Red Delicious") || !aType.equalsIgnoreCase("Golden Delicious") || !aType.equalsIgnoreCase("Gala") || !aType.equalsIgnoreCase("Granny Smith"))
{
System.out.println ("That is an invalid type of apple");
return;
}
this.type = aType;
}
public void setWeight (double aWeight)
{
if (aWeight < 0 || aWeight > 2)
{
System.out.println("That is an invalid weight");
return;
}
this.weight = aWeight;
}
public void setPrice (double aPrice)
{
if (aPrice < 0)
{
System.out.println("That is an invalid price");
return;
}
this.price = aPrice;
}
//Methods
public String toString()
{
return "Name: " + type + " Weight " + weight + " Price " + price;
}
public boolean equals (Apple aApple)
{
return this.type.equalsIgnoreCase (aApple.getType()) && this.weight == aApple.getWeight() && this.price == aApple.getPrice();
}
ここに私のAppleクラスに呼びかけリンゴテスターのコードがあります:出力で
System.out.println("Setting the new apple's values to the following valid values: Granny Smith, 0.75, 0.99\nPrinting the new apple's values");
Apple grannySmith = new Apple();
grannySmith.setType("Granny Smith");
grannySmith.setWeight (0.75);
grannySmith.setPrice (0.99);
System.out.println(grannySmith+ "\n");
、それが無効な型であると言いますまた何らかの理由でリンゴの名前が変わってしまいます。そして、Galaがデフォルトである "Name:Gala"を設定し、名前を "Granny Smith"に変更しません。リンゴの無効なタイプでグラニースミス、0.75、0.99
新しいAppleの値を印刷
:別のリンゴ、次の有効な値に新しいAppleの値を設定
を作成
名前:ガラ重量0.75価格0.99
なぜそれはそれが無効なタイプのリンゴであるのかわかりませんし、なぜ私がそれを設定するのではなく、デフォルトのリンゴタイプとして名前を印刷するのか分かりません。たぶん私のミューテータif文が間違っていますか?
、このような単純なミスをすごいああ、ありがとう! –