- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
今天我正在开发一个新程序,用于在充满数据的文件中找到最便宜的披萨。不过,我完全被困在一个问题上,它正在使用 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/
这似乎很奇怪,这并没有像我预期的那样工作。我编写了一个简单的java类,它实现Comparable接口(interface)并重写compareTo()方法。但是,它不允许我传递除对象之外的特定类型的
public int compareTo(Object another) throws CustomMadeException { if(this.getClass() != another.
早些时候,我在通用 IComparable 中获得了逆变的具体示例Jon Skeet 的界面。然而,这又产生了另一个问题。为什么不是通用的 List.Sort()方法能够推断出相同的信息? 我在这里提
我有注释 package javaannotationtest; import java.lang.annotation.*; @Target({ElementType.METHOD}) @Reten
我试图理解 C# 中的 CompareTo(),下面的示例让我比以往任何时候都更加困惑。有人能帮我理解为什么第三个变体的结果是 1 吗?句子“Hello wordd”中的第二个词与 str1“Hell
我已经在这个程序上工作了几天,并且在我的 BinarySearchTree 类中实现了一些主要方法,例如插入和删除。插入似乎工作正常,但是一旦我尝试删除,我就会不断出错。因此,在玩弄了代码之后,我想测
首先,我必须提到的是,在阅读了大量问题和教程并观看了一些视频后,问题仍然没有解决。 我是一名Java中级程序员,我编写了一些用于比较优先级队列中的元素的代码,其中元素的保存方式类似于[Comparab
我只是在为即将到来的考试做一些复习。我发现了我们的讲师给我们提供的这段代码供我们修改。 代码: public class Employee implements Comparable{ private
我的书要求我为一段代码编写 Javadoc 注释。大多数情况下,我了解如何执行 javadocs,但我不了解该程序在做什么。 “为类 Person 的以下方法编写 Javadoc 注释。假设类 Per
如果我写以下内容是否有可能溢出: public class SomeObj implements Comparable { private final float data; pub
我有一个名为任务的类,我想将其放入 PriorityQueue 中。 我的类(class)通过日期和名为isUrgent的 boolean 字段进行比较 @Override publ
这个问题已经有答案了: Java error: Comparison method violates its general contract (13 个回答) 已关闭 7 年前。 我有这个compa
我的类(class)结构: public class Priorityy implement Comparable { public int compareTo(Object pe) {
给定一个非负整数列表,我想对它们进行排列,使它们形成最大的数字。给定 [1, 20, 23, 4, 8],最大的形成数字是 8423201。但我想弄清楚compareTo 方法中变量的顺序如何影响 A
我想通过上次联系日期比较两个“收件人”,如果相同,则通过地址进行比较。这是我的代码: public class RecipientComparator implements Comparator {
package Comparable; public class Movie implements Comparable { private double rating; privat
您好,我在实现compareTo 方法时遇到问题。我一直在寻找答案,但没有任何帮助。我正在尝试用各种大小的圆圈填充 TreeSet。我需要在我的圈子类中使用compareTo 才能以这种方式存储它们。
我有以下代码;目的是返回数组中按字母顺序排列的最小成员。 public String smallest() { String smallest = ""; int i = 0; while(log[i
我有一个关于compareTo函数如何协助比较器进行排序的问题即 o1.compareTo(o2) 与 o2.compareTo(o1) 如果两个字符串相等,则此方法返回 0,否则返回正值或负值。如果
帮助我无法弄清楚compareTo函数。这就是我必须做的:编写一个compareTo函数,可用于根据以下内容按顺序放置产品到他们的零件号。也就是说,后面按字母顺序排列的零件号顺序大于按字母顺序排列较早
我是一名优秀的程序员,十分优秀!