为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

牧羊人nacy 2022-07-06 15:55:36
我的代码由以下方面和目标组成: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);
查看完整描述

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

    }

}



查看完整回答
反对 回复 2022-07-06
  • 1 回答
  • 0 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信