java - 读取用户输入到列表,根据元素值排序,并按顺序返回数组中的列表元素 - Java

标签 java

我的代码由以下方面和目标组成: 1) 获取三个设备的用户输入 2)将这些输入存储在列表中 3)将列表存储在数组中 4) 根据 ROI 的较大值对数组内的列表进行排序(数组中每个列表的索引 3) 5) 按最大 ROI 的顺序返回数组中的列表

为此,我创建了三个类文件:Main、Equipment 和 Sort

我的问题: 1)我的 set() 方法返回类型未定义的错误 2)我的比较工具按升序返回值,但我需要降序(从大到小)

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Equipment equ1 = new Equipment("Name", 1.00, 2.00, 3.00); 
        Equipment equ2 = new Equipment("Name", 1.00, 2.00, 3.00); 
        Equipment equ3 = new Equipment("Name", 1.00, 2.00, 3.00); 
        
    //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.set(0, equName1);

        System.out.println("Enter Equipment Set 1 Gain: ");
        Double equGain1 = input.nextDouble(); 
        equ1.set(1, equGain1); 

        System.out.println("Enter Equipment Set 1 Cost: ");
        Double equCost1 = input.nextDouble(); 
        equ1.set(2, equCost1);

        double roi1 = (equGain1 - equCost1) / equCost1; 
        equ1.set(3, 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.set(0, equName2);

        System.out.println("Enter Equipment Set 2 Gain: ");
        Double equGain2 = input.nextDouble();
        equ2.set(1, equGain2);

        System.out.println("Enter Equipment Set 2 Cost: ");
        Double equCost2 = input.nextDouble();
        equ2.set(2, equCost2);

        double roi2 = (equGain1 - equCost1) / equCost1;
        equ2.set(3, roi2);

    //Equipment Set 3 from User Input
        System.out.println("Enter Equipment Set 3 Name: ");
        String equName3 = input.nextLine();
        equ3.set(0, equName3);

        System.out.println("Enter Equipment Set 3 Gain: ");
        Double equGain3 = input.nextDouble();
        equ3.set(1, equGain3);

        System.out.println("Enter Equipment Set 3 Cost: ");
        Double equCost3 = input.nextDouble();
        equ3.set(2, equCost3);

        double roi3 = (equGain1 - equCost1) / equCost1;
        equ3.set(3, roi3);
 
        List<Equipment> equipment = new ArrayList<Equipment>();
        equipment.add(equ1); //Add each list to the array list
        equipment.add(equ2);
        equipment.add(equ3);

        Collections.sort(equipment, new Sort()); 
        System.out.println(equipment); 
    }
}

设备.java

public class Equipment { //This class seeks to define the elements and the structure of Equipment
    String equName;
    double equGain;
    double equLoss;
    double roi;

    public Equipment(String equName, double equGain, double equLoss, double roi) {
        this.equName = equName;
        this.equGain = equGain;
        this.equLoss = equLoss;
        this.roi = roi;

    }

    public String toString() {
        return "Equipment: " + equName + " Gain:  " + equGain + "Loss: " + equLoss + "ROI: " + roi;
    }
}

排序.java

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(o1.getroi() - o2.getroi()); //I want to compare the ROI value of each list
    }
}

最佳答案

您的主要问题是缺乏面向对象编程的知识。我建议了解这个范例是如何工作的。 您的代码中有多个问题,所以让我们逐步解决:

对于第一个问题,您似乎没有为模型定义 setter 和 getter 方法。定义它们并将类的属性设为私有(private)。

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 相减。您将两个 double 值作为方法的参数。您应该交换方法参数以获得升序或降序: 例如:

Double.compare(obj1.getRoi(),obj2.getRoit()) --> ascending order
Double.compare(obj2.getRoi(),obj1.getRoit()) --> descending order

排序.class

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());
    }
}

在你的 main 中,你混淆了变量。对于每个用户输入,您计算的第一个对象是 ROI,而不是其他对象。除了它们之外,set方法应该替换为每个设备属性的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);
    }
}

关于java - 读取用户输入到列表,根据元素值排序,并按顺序返回数组中的列表元素 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54850170/

相关文章:

java - 面试问题(C#)

Java 正则表达式帮助

java - 打乱 LinkedList 时出现 ArrayIndexOutOfBoundsException

java - 处理海量数据时,在数据存储中定义实体的正确方法是什么?

java - 从 HttpPost Android 中提取 URL

java - 尝试使用 javac 编译 .java helloworld 程序时出错

java - 二叉搜索树中的重复项

java - Predictionio pio 构建成功,但 pio 训练错误,未找到 'name'

java - 如何更改确认对话框中的是/否选项?

JavaMail STARTTLS 错误?