对 Java 中的用户输入进行排序

Sorting the User input in Java

我是 Java 的初学者。我需要帮助来继续我的代码。提前致谢。

问题:给定一个未排序的 5 名运动员提名教练名单 class,为教练提供一种方法来搜索运动员姓名并提供成绩。最后按姓名排序打印运动员姓名列表及其成绩。搜索成绩最高的运动员。


package student;
import java.util.Scanner;
public class Atheletes {
    String name;
    static String  grade,grade1,grade2,grade3,grade4;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the name of athelete1 and grade");
        grade1 = in.nextLine();
        Scanner ino = new Scanner(System.in);
        System.out.println("Enter the name of athelete2 and grade");
        grade2 = ino.nextLine();
        Scanner ine = new Scanner(System.in);
        System.out.println("Enter the name of athelete3and grade");
        grade3 = ine.nextLine();
        Scanner inp = new Scanner(System.in);
        System.out.println("Enter the name of athelete4 and grade");
        grade4 = inp.nextLine();
    }
}
public class Athletes {

private String name;
private String grade;

public Athletes(String name, String grade) {
    this.name = name;
    this.grade = grade;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getGrade() {
    return grade;
}

public void setGrade(String grade) {
    this.grade = grade;
}

@Override
public String toString() {
    return "Athletes [name=" + name + ", grade=" + grade + "]";
}

public static void main(String[] args) {
    List<Athletes> lijst = new ArrayList<Athletes>();
    lijst.add(new Athletes("bbb", "Grade1"));
    lijst.add(new Athletes("ccc", "Grade2"));
    lijst.add(new Athletes("aaa", "Grade3"));
    lijst.add(new Athletes("ddd", "Grade4"));
    Collections.sort(lijst, new Comparator<Athletes>() {
        @Override
        public int compare(Athletes o1, Athletes o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });

    for (Athletes athletes : lijst) {
        System.out.println(athletes);
    }
}

}

好的,因为您可以使用数组和 for 循环但不能使用集合:

public class Sorter(){

    private int[] grades = {7, 6, 4, 10, 8};
    private String[] names = {"John", "Erik", "Bob", "Frank", "Judy"};

    public static void main(String args[]) {
        new Sorter();
    }

    public Sorter(){
        int[] tempGrades = {0, 0, 0, 0, 0};
        String[] tempNames = {"", "", "", "", ""};
        for (int x = 0; x < tempGrades.length; x++) {
            if (grades[x] < tempGrades[1]) {
                tempGrades[0] = grades[x];
                tempNames[0] = names[x];
            } else if (grades[x] < tempGrades[2]) {
                tempGrades[0] = tempGrades[1];
                tempGrades[1] = grades[x];
                tempNames[0] = tempNames[1];
                tempNames[1] = names[x];
            } else if (grades[x] < tempGrades[3]) {
                tempGrades[0] = tempGrades[1];
                tempGrades[1] = tempGrades[2];
                tempGrades[2] = grades[x];
                tempNames[0] = tempNames[1];
                tempNames[1] = tempNames[2];
                tempNames[2] = names[x];
            } else if (grades[x] < tempGrades[4]) {
                tempGrades[0] = tempGrades[1];
                tempGrades[1] = tempGrades[2];
                tempGrades[2] = tempGrades[3];
                tempGrades[3] = grades[x];
                tempNames[0] = tempNames[1];
                tempNames[1] = tempNames[2];
                tempNames[2] = tempNames[3];
                tempNames[3] = names[x];
            } else {
                tempGrades[0] = tempGrades[1];
                tempGrades[1] = tempGrades[2];
                tempGrades[2] = tempGrades[3];
                tempGrades[3] = tempGrades[4];
                tempGrades[4] = grades[x];
                tempNames[0] = tempNames[1];
                tempNames[1] = tempNames[2];
                tempNames[2] = tempNames[3];
                tempNames[3] = tempNames[4];
                tempNames[4] = names[x];
            }
        }
        grades = tempGrades;
        names = tempNames;
        for (int x = 0; x < grades.length; x++) {
            System.out.println(tempNames[x] + " " + tempGrades[x]);
        }
    } 
}

只为未来:

您可以使用 ArrayList<Athlete>,其中 Athlete 是一个 class,它接受 (String name, int grade) 作为构造函数参数并通过实现自己的比较器按等级对运动员进行排序,或者您可以使用 LinkedHashMap<Integer, String>Key<Integer> 对值进行排序。

注意:Class 带有复数形式的名称(如 Athletes)最适用于仅实现静态方法和变量的单例 classes。始终按功能命名 classes(在本例中为排序),AthleteSorter 也是可行的。

您可以编写自己的比较器 Class 以根据运动员的姓名对运动员进行排序

public class AtheleteComparator implements Comparator
{
         @override
         public int compare(Atheletes first,Atheletes second)
       {
            return first.name.compareTo(second.name);
       }
}

然后只需使用

Collections.sort(List<Athelete>list,Your own Comparator's object)

要找出得分最高的运动员,请编写另一个比较器来比较 成绩

然后使用

Collections.sort(arrayList,Comparator); // Sort the arraylist
arrayList.get(arrayList.size() - 1); //gets the last item, largest for an ascending sort

我已经简化了您的代码并根据需要添加了注释。

    // number of Athletes you want
    Athlete[] eAthlete = new Athlete[5];
    // Name of each athlete
    String[] names = { "ss", "aa", "bb", "cc", "xx" };

    // On each iteration, the name of the Athlete
    // and his/her grade is set,

    Scanner in = new Scanner(System.in);
    for (int i = 0; i < eAthlete.length; i++) {
        eAthlete[i] = new Athlete();
        eAthlete[i].setName(names[i]);
        System.out.println("Please enter Grade for: "
                + eAthlete[i].getName());
        eAthlete[i].setGrade(in.nextLine());
    }

    in.close();

    // Print all athletes with their grades,
    System.out.println("Before Sorting");
    for (Athlete s : eAthlete) {
        System.out.println(s.getName() + " " + s.getGrade());
    }

至此,成绩和姓名分配给每个运动员,

输出

Before Sorting
ss 123
aa 65465
bb 4654
cc .0231
xx 23123

现在我们需要根据他们的名字对这些运动员进行排序。

我们本可以设计自己的 Comparator,但由于不允许您使用 Collections.sort,我们会使用相当糟糕的方法,即冒泡排序,

 String tempStr;
 for (int t=0; t<eAthlete.length-1; t++)
    {
        for (int i= 0; i < eAthlete.length - t -1; i++)
        {
            if(eAthlete[i+1].getName().compareTo(eAthlete[i].getName())<0)
            {
                tempStr = eAthlete[i].getName();
                eAthlete[i].setName(eAthlete[i+1].getName());
                eAthlete[i+1].setName(tempStr);
            }
        }
    }

打印排序的运动员及其成绩,

System.out.println("After Sorting");
         for (Athelete s : eAthelete){
             System.out.println(s.getName() + " " + s.getGrade());
         }

输出:

After Sorting

aa 65465
bb 4654
cc .0231
ss 123
xx 23123

观察上面输出中的名字。

这是你的 Athlete class,

class Athlete {

    private String name;
    private String grade;


    public void setName(String name) {
        this.name = name;
    }

    public void setGrade(String gr) {
        grade = gr;
    }

    public String getGrade() {
        return grade;
    }

    public String getName() {
        return name;
    }
}

完整代码如下,

public class Main {
    public static void main(String[] args) {

        Athlete[] eAthlete = new Athlete[5];
        String[] names = { "ss", "aa", "bb", "cc", "xx" };

        Scanner in = new Scanner(System.in);
        for (int i = 0; i < eAthlete.length; i++) {
            eAthlete[i] = new Athlete();
            eAthlete[i].setName(names[i]);
            System.out.println("Please enter Grade for: "
                    + eAthlete[i].getName());
            eAthlete[i].setGrade(in.nextLine());
        }

        in.close();

        // Print all athletes with their grades,
        System.out.println("Before Sorting");
        for (Athlete s : eAthlete) {
            System.out.println(s.getName() + " " + s.getGrade());
        }

        String tempStr;

        for (int t = 0; t < eAthlete.length - 1; t++) {
            for (int i = 0; i < eAthlete.length - t - 1; i++) {
                if (eAthlete[i + 1].getName().compareTo(eAthlete[i].getName()) < 0) {
                    tempStr = eAthlete[i].getName();
                    eAthlete[i].setName(eAthlete[i + 1].getName());
                    eAthlete[i + 1].setName(tempStr);
                }
            }
        }

        System.out.println("After Sorting");
        for (Athlete s : eAthlete) {
            System.out.println(s.getName() + " " + s.getGrade());
        }
    }
}

class Athlete {

    private String name;
    private String grade;

    public void setName(String name) {
        this.name = name;
    }

    public void setGrade(String gr) {
        grade = gr;
    }

    public String getGrade() {
        return grade;
    }

    public String getName() {
        return name;
    }

}