【问题标题】:compareTo Method with Doubles[Double Cannot Be Dereferenced]带双精度的 compareTo 方法[无法取消引用双精度]
【发布时间】:2014-12-17 16:33:47
【问题描述】:

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

B.廉价披萨.java 一种。 Objective – 在 Pizza.java 中实现 Comparable 接口。 湾。作业——使用你复制到第 10 章的 Pizza.java 文件。实现 Comparable 接口并添加 compareTo() 方法。应该使用这个 compareTo() 帮助您在 Pizza.txt 中找到最便宜的披萨。 编写CheapPizza.java 客户端类来测试新版本的Pizza.java。便宜的披萨会 从文件中读取比萨饼,并使用 compareTo() 方法。创建一个 Pizza 对象来保存最便宜的 Pizza 和一个 Pizza 对象 从文件中读取比萨饼。使用 compareTo() 比较最便宜的披萨和 比萨从文件中读取。如果从文件中读取的比萨更便宜,则更改最便宜的 比萨。 一种。输入 – Blackboard 上的 Pizzas.txt 文件。 湾。输出 - 应完全以如下所示的格式出现: 最便宜的披萨:9英寸橄榄披萨售价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;
    }
}

【问题讨论】:

  • 您不能将compareTo 与原语一起使用,doubles 是。只需直接比较它们,例如this.cost &lt; temp.cost.
  • 非常感谢您提供的信息!

标签: java computer-science compareto


【解决方案1】:
       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 或者你简单地使用算术运算。

【讨论】:

  • 感谢您的帮助,我从来没有被教过这个,我很高兴有人能够向我解释这个!
【解决方案2】:

cost 是基本类型double 的字段,您不能在基本类型上调用方法(在您的情况下为compareTo()),只能在对象上调用。要比较两个双打,直接比较他们就行了

 if (this.cost < temp.cost)
    return -1;
 else if(this.cost > temp.cost)
    return 1;
 else
    return 0;

【讨论】:

  • 这将修复一个错误,但他们仍然会遇到 temp.cost 是私有变量且无法通过 compareTo 方法访问的问题。
  • 不,因为它也是Pizza 类的一个方法,因此可以访问它的私有变量。
  • 哦,我想可以。我的错误
【解决方案3】:

你必须为 Pizza 类实现 java.lang.Comparable 接口

public class Pizza implements Comparator<Pizza> {
    // Enter other codes
    // with toString

   @Override
   public int compareTo(Pizza o) {
        return  Double.valueOf(this.cost).compareTo(Double.valueOf(o.cost));
   }
}

然后使用 Collections.sort(listOfPizzas) 根据成本对披萨进行排序。

public static void main(String[] args) {
    List l = new ArrayList();
    l.add(new Pizza(9, 7.99, "Olive Pizza"));
    l.add(new Pizza(9, 8.99, "Hot & Spicy Chicken Pizza"));
    l.add(new Pizza(9, 9.99, "Corned Mutton Sensation Pizza"));
    Collections.sort(l);
    System.out.println(" " + l.get(0));
}

【讨论】:

  • 感谢您让我知道这种数据排序的新方法,非常感谢!
猜你喜欢
  • 2016-02-21
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
相关资源
最近更新 更多