读取用户输入到列表,根据元素值排序,并按顺序返回数组中的列表元素 - 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);



牧羊人nacy
浏览 96回答 1
1回答

繁星点点滴滴

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

相关分类

Java