Java:字符串方法未正确修改?

标签 java

我的任务是创建一个苹果程序,其参数是苹果的类型只能是“Red Delicious”、“Golden Delicious”、“Gala”和“Granny Smith”。

但是,由于某种原因,即使我调用类然后将苹果类型设置为“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”(Gala 是默认值),并且不会将名称更改为“Granny Smith”。

Creating another apple

Setting the new apple's values to the following valid values: Granny Smith, 0.75, 0.99

Printing the new apple's values

That is an invalid type of apple

Name: Gala Weight 0.75 Price 0.99

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

最佳答案

您应该使用 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”,因此您的原始语句会将其视为无效,并且在第一次检查时失败。

您可以通过将其更改为来简化 boolean 条件以使其更具可读性:

!(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)) { ... }

关于Java:字符串方法未正确修改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40098425/

相关文章:

java - 格雷厄姆扫描的问题

java - MVC-将字符串从 servlet 传递到业务逻辑代码

java - 在java中管理配置(初始配置/保存/加载配置)

java - 为什么我的简化 DES 实现在 Cp1252 编码下工作正常,但在 UTF-8 编码下工作不佳?

java - BitmapFactory.decodeResource 在 Android 2.2 中返回一个可变位图,在 Android 1.6 中返回一个不可变位图

java - MongoDb 将 Float 字段存储为 Double。获取 IllegalArgumentException

java - 无法在 Android Studio/Java 中将字符串转换为整数

java - 服务器套接字接受的第三个请求是什么?

java - close()方法的使用(Java初学者)

java - 在Linux服务器上编译Android .apk