gpt4 book ai didi

java - 使用 double 的 compareTo 方法 [无法取消引用 double ]

转载 作者:行者123 更新时间:2023-12-02 15:30:18 27 4
gpt4 key购买 nike

今天我正在开发一个新程序,用于在充满数据的文件中找到最便宜的披萨。不过,我完全被困在一个问题上,它正在使用 compareTo 方法。我在我的资源类中创建了它,但我没有收到错误消息说不能取消引用 double,我查看了这个问题但仍然没有帮助。我不是最高级的程序员,不理解其他资源上的许多复杂答案。我对该程序的规范如下:

B. CheapPizza.java a. Objective – implement the Comparable interface in Pizza.java. b. Assignment – Use the Pizza.java file you copied into Chapter 10. Implement the Comparable interface and add a compareTo() method. This compareTo() should be used to help you find the cheapest pizza in pizza.txt. Write CheapPizza.java client class to test the new version of Pizza.java. CheapPizza will read in the pizzas from the file and will keep track of the cheapest pizza by using the compareTo() method. Create a Pizza object to hold the cheapest pizza and a Pizza object to read in the pizzas from the file. Use compareTo() to compare the cheapest pizza to the pizza read from the file. If the pizza read from the file is cheaper, change the cheapest pizza. a. Input – pizzas.txt file on Blackboard. b. Output – should appear exactly in the format shown below: The cheapest pizza: The 9 inch olive pizza will cost $7.99

compareTo 方法的代码在我代码的底部,有人可以向我解释我做错了什么吗?感谢您的宝贵时间,祝您有美好的一天! ~

public class Pizza   {   
private int size;
private double cost;
private String topping;

public Pizza(int s, double c, String t)
{
size = s;
cost = c;
topping = t;
}
public Pizza(String t, int s, double c) //.equals Method For Comparing Pizza for ''PizzaMatch''
{
topping = t;
size = s;
cost = c;

}

public Pizza()
{
size = 10;
cost = 9.00;
topping = "Cheese";
}

public String toString()
{
return String.format("%d inch %s pizza will cost $%.2f", size, topping, cost);
}
public int getSize()
{
return size;
}
public void setSize(int s)
{
size = s;
}

public double getCost()
{
return cost;
}
public void setCost(Double c)
{
cost = c;
}
public String getTopping()
{
return topping;
}
public void setTopping(String t)
{
topping = t;
}

public boolean equals(Object obj) //.equals Method For Comparing Pizza in "PizzaMatch"
{
if(!(obj instanceof Pizza))
throw new IllegalArgumentException("Parameter must be of Pizza!");

Pizza temp = (Pizza) obj;

if (this.topping.equals(temp.topping) && this.size == temp.size && this.cost == temp.cost)
return true;
else
return false;

}
//============================================================================================
public int compareTo(Object obj){
if(!(obj instanceof Pizza))
throw new IllegalArgumentException
("Parameter must be a Pizza");
Pizza temp = (Pizza) obj;
if (this.cost.compareTo(temp.cost) < 0) //this comes 1st
return -1;
else if(this.cost.compareTo(temp.cost) > 0) //temp comes 1st
return 1;
else //this and temp are equal
return 0;
}
}

最佳答案

       if(!(obj instanceof Pizza))
throw new IllegalArgumentException
("Parameter must be a Pizza");
Pizza temp = (Pizza) obj;
return Double.valueOf(this.cost).compareTo(Double.valueOf(temp.cost));

成本是原始的两倍。 CompareTo 是对象中的一个方法。所以你可以使用 Double 对象而不是 double 或者你简单地使用算术运算。

关于java - 使用 double 的 compareTo 方法 [无法取消引用 double ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27530332/

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