1 回答
TA贡献1803条经验 获得超3个赞
您的主要问题是缺乏面向对象编程的知识。我建议了解这种范式是如何工作的。您的代码中有多个问题,所以让我们一步一步来:
对于第一个问题,您似乎没有为模型定义 setter 和 getter 方法。定义它们并将类的属性设为私有。
public class Equipment {
private String equName;
private double equGain;
private double equLoss;
private double roi;
public Equipment() {
}
public Equipment(String equName, double equGain, double equLoss, double roi) {
this.equName = equName;
this.equGain = equGain;
this.equLoss = equLoss;
this.roi = roi;
}
public String getEquName() {
return equName;
}
public void setEquName(String equName) {
this.equName = equName;
}
public double getEquGain() {
return equGain;
}
public void setEquGain(double equGain) {
this.equGain = equGain;
}
public double getEquLoss() {
return equLoss;
}
public void setEquLoss(double equLoss) {
this.equLoss = equLoss;
}
public double getRoi() {
return roi;
}
public void setRoi(double roi) {
this.roi = roi;
}
public String toString() {
return "Equipment: " + equName + " Gain: " + equGain + "Loss: " + equLoss + "ROI: " + roi;
}
}
您的比较器方法使用 Double.compare(double obj1,double obj2) 进行比较。你用的方法不对。您不必将两个双打相减。您将两个双精度值作为方法的参数。您应该交换方法参数以获得升序或降序:例如:
Double.compare(obj1.getRoi(),obj2.getRoit()) --> ascending order
Double.compare(obj2.getRoi(),obj1.getRoit()) --> descending order
排序类
import java.util.Comparator;
public class Sort implements Comparator<Equipment> { //Implementing a comparison tool for lists
@Override
public int compare(Equipment o1, Equipment o2) { //I want to compare one list to another - Update from Java 8
return Double.compare(o2.getRoi(), o1.getRoi());
}
}
在您的主要内容中,您混淆了变量。对于每个用户输入,您计算的第一个对象是 ROI,而不是其他对象。除了它们之外,set 方法应该替换为每个 Equipment 属性是 setter 方法。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ArrayTest {
public static void main(String[] args) {
Equipment equ1 = new Equipment();
Equipment equ2 = new Equipment();
Equipment equ3 = new Equipment();
//Equipment Set 1 from User Input
System.out.println("Enter Equipment Set 1 Name: ");
Scanner input = new Scanner(System.in);
String equName1 = input.nextLine();
equ1.setEquName(equName1);
System.out.println("Enter Equipment Set 1 Gain: ");
Double equGain1 = input.nextDouble();
equ1.setEquGain(equGain1);
System.out.println("Enter Equipment Set 1 Cost: ");
Double equCost1 = input.nextDouble();
equ1.setEquLoss(equCost1);
double roi1 = (equGain1 - equCost1) / equCost1;
equ1.setRoi(roi1); //Place ROI at index 3
//Equipment Set 2 from User Input
System.out.println("Enter Equipment Set 2 Name: ");
String equName2 = input.nextLine();
equ2.setEquName(equName2);
System.out.println("Enter Equipment Set 2 Gain: ");
Double equGain2 = input.nextDouble();
equ2.setEquGain(equGain2);
System.out.println("Enter Equipment Set 2 Cost: ");
Double equCost2 = input.nextDouble();
equ2.setEquLoss(equCost2);
double roi2 = (equGain2 - equCost2) / equCost2;
equ2.setRoi(roi2);
//Equipment Set 3 from User Input
System.out.println("Enter Equipment Set 3 Name: ");
String equName3 = input.nextLine();
equ3.setEquName(equName3);
System.out.println("Enter Equipment Set 3 Gain: ");
Double equGain3 = input.nextDouble();
equ3.setEquGain(equGain3);
System.out.println("Enter Equipment Set 3 Cost: ");
Double equCost3 = input.nextDouble();
equ3.setEquLoss(equCost3);
double roi3 = (equGain3 - equCost3) / equCost3;
equ3.setRoi(roi3);
List<Equipment> equipment = new ArrayList<>();
equipment.add(equ1); //Add each list to the array list
equipment.add(equ2);
equipment.add(equ3);
equipment.sort(new Sort());
System.out.println(equipment);
}
}
添加回答
举报