gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:22:15 27 4
gpt4 key购买 nike

我的任务是创建一个苹果程序,其参数是苹果的类型只能是“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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com