Java:String方法无法正确修改?

时间:2016-10-18 02:08:36

标签: java

我的任务是创建一个带有某个参数的苹果程序,苹果的类型只能是" Red Delicious"," Golden Delicious"," Gala&# 34;和"格兰尼史密斯"。

但是,出于某种原因,即使我打电话给班级,然后将苹果类型设置为" Granny Smith"我仍然得到一个"这是一种无效的苹果类型"。此外,它不会修改" Gala"中的默认类型名称。也许我的if陈述错了?

以下是Apple课程:

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类的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&#34; Gala是默认的,并且它不会将名称更改为&#34; Granny Smith&#34;。

  

创建另一个苹果

     

将新苹果值设置为以下有效值:Granny Smith,0.75,0.99

     

打印新苹果值

     

这是一种无效的苹果类型

     

名称:嘎拉重量0.75价格0.99

我不知道为什么它说这是一种无效的苹果类型以及为什么它将名称打印为默认的苹果类型而不是我设置的类型。也许我的mutator if语句错了?

1 个答案:

答案 0 :(得分:1)

你应该使用AND(&&)而不是OR(||)。如果所有条件都是true,则您希望打印错误消息,而不是只有其中一个是true

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;
}

考虑您的类型为"Gala"的情况。这不等于"Red Delicious",因此您的原始陈述会将其视为无效,并在第一次检查时失败。

您可以通过将布尔条件更改为:

来简化布尔条件以使其更具可读性
!(aType.equalsIgnoreCase("Red Delicious") 
        || aType.equalsIgnoreCase("Golden Delicious") 
        || aType.equalsIgnoreCase("Gala") 
        || aType.equalsIgnoreCase("Granny Smith"))

或者,更好的是:

List<String> apples = Arrays.asList({ "Red Delicious", "Golden Delicious", "Gala", "Granny Smith" });

if (apples.contains(aType)) { ... }