如何从数组

时间:2015-04-27 02:26:43

标签: java

我必须制作一个程序,让用户输入任意数量的学生,询问每个学生的姓名和成绩。所以,如果我说2个学生,我会把比利史密斯,然后是54,那么它会问我第二个学生的名字,约翰史密斯,然后是等级,81。然后它输出等级降序的等级名称。它会输出:

name---------grades
------------------
John smith     81

billy smith   54

除了将它打印出来之外,我还有其他的东西。我需要它用等级打印出名字。这就是我所拥有的:

import java.util.*;

public class assignment5 {

public static void main(String[] args) {

//      Scanner for first name and last name with space in between.

java.util.Scanner input = new java.util.Scanner(System.in);

input.useDelimiter(System.getProperty("line.separator"));

        System.out.print("Enter the number of students: ");
        int numofstudents = input.nextInt();
        String[] names = new String[numofstudents];
        Double[] array = new Double[numofstudents];
        for(int i = 0; i < numofstudents; i++) {
            System.out.print("Enter the student's name: ");
            names[i] =input.next();
            System.out.print("Enter the student's score: ");
            array[i] = (Double) input.nextDouble();
        }
        System.out.print("Name" + "\tScore");
        System.out.print("\n----" + "\t----\n");
        selectionSort(names, array);
        System.out.println(Arrays.toString(names));

    }
    public static void selectionSort(String[] names, Double[] array) {
        for(int i = array.length - 1; i >= 1; i--) {
            String temp;
            Double currentMax = array[0];
            int currentMaxIndex = 0;
            for(int j = 1; j <= i; j++) {
                if (currentMax > array[j]) {
                    currentMax = array[j];
                    currentMaxIndex = j;
                }
            }       
            if (currentMaxIndex != i) {

                temp = names[currentMaxIndex];
                names[currentMaxIndex] = names[i];
                names[i] = temp;
                array[currentMaxIndex] = array[i];
                array[i] = currentMax;
            }
        }       
    }
}

2 个答案:

答案 0 :(得分:0)

这与价值基础排序地图非常相似。以下提供的解决方案会有所帮助。

Sort a Map<Key, Value> by values (Java)

答案 1 :(得分:0)

这是一种方法,创建一个Student类来存储名称/成绩对,并使用Arrays.sort()和自定义Comparator在所有输入后按降序排序读:

import java.util.*;

public class assignment5 {

public static void main(String[] args) {

//      Scanner for first name and last name with space in between.

        java.util.Scanner input = new java.util.Scanner(System.in);

        input.useDelimiter(System.getProperty("line.separator"));

        System.out.print("Enter the number of students: ");
        int numofstudents = input.nextInt();
        //String[] names = new String[numofstudents]; //not needed
        //Double[] array = new Double[numofstudents]; //not needed
        Student[] students = new Student[numofstudents]; //added

        for(int i = 0; i < numofstudents; i++) {
            System.out.print("Enter the student's name: ");
            //names[i] =input.next();
            String student = input.next(); //added

            System.out.print("Enter the student's score: ");
            //array[i] = (Double) input.nextDouble();
            Double grade = (Double) input.nextDouble(); //added

            students[i] = new Student(student, grade); //added
        }
        System.out.print("Name" + "\tScore");
        System.out.print("\n----" + "\t----\n");
        //selectionSort(names, array);

        Arrays.sort(students, new Comparator<Student>() {

          @Override
          public int compare(Student entry1, Student entry2) {
            return entry2.grade.compareTo(entry1.grade); //sort by grade in descending order
          }
        });

        //System.out.println(Arrays.toString(students));
        for (int i = 0; i < students.length; i++){
          System.out.println(students[i].student + "\t" + students[i].grade);
        }

    }

}

public class Student{
  public String student;
  public Double grade;
  public Student(String s, Double g){
    this.student = s;
    this.grade = g;
  }
}