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);
}
}
添加回答
舉報