如何在程序中包含排序方法?

How to include a sorting method in program?

以下是该计划的要求。本质上,输出必须如下所示,同时仍遵守以下准则。我的问题是如何更改我的程序以满足要求 #4(正在编写一种方法,该方法获取一维和二维数组中的学生数据并打印学生记录:姓名、2 个测验分数和平均分。输出字段应为 well-aligned。)我的第二个问题是如何满足要求 #6(应在学生记录之前打印 header 行,以及一行“------- ----------“应该每 2 个学生打印一次)。

 # Name    Q1   Q2         

  1. name1 ... ... ...

  2. name2 ... ... ...


  1. name3 ... ... ...

  2. name4 ... ... ...


  1. 姓名 5 ... ... ...

============================================= ===================================== 1. 在 main() 方法中,定义一个一维数组,可以存储 5 个学生姓名。在 main() 中定义一个二维数组,存储 5 个学生(行)的考试记录,每个学生有 3 个数据字段(列):2 个测验分数和 2 个测验的平均值。

  1. 将学生姓名(真实姓名)分配给一维数组,将 2 个测验分数分配给二维数组。

  2. 计算每个学生 2 次测验的平均值。

4.编写一个获取学生数据(一维和二维数组)的方法,并按平均字段从高到低对学生记录进行排序。

  1. 编写一个获取学生数据(一维和二维数组)并打印学生记录的方法:姓名、2 个测验分数和平均分。输出字段应为 well-aligned.

6.学生记录前打印header行,每2名学生打印一行“----------------”。

  1. 下面的输出是一个例子:

名称 Q1 Q2 平均值


  1. name1 ... ... ...

  2. name2 ... ... ...


  1. name3 ... ... ...

  2. name4 ... ... ...


  1. 姓名 5 ... ... ...

============================================= ====================================

这是我目前所拥有的。我真的对排序方法有问题,每 2 个学生打印一行“----------------”。

public class 学生成绩 {

public static void main(String[] args) {

    //create variables to be used inside the "for" loops as counters
    int i;
    int row;
    int column;

    String[] students = {"Peter", "Lydia", "Kate", "Steve", "Alexa"};



    // Create a 2-D array of the int type to stores scores
    int[][] scores = { {82, 90}, {78,90}, {84, 80}, {85, 73}, {81, 93} };


    // Display headings for information to be spaced equally
    System.out.printf("%-7s%-7s%-7s%-7s%-7s", "Name",
            "Test1", "Test2", "Sum of tests ", "Average grade\n");
    System.out.print("----------------------------------------\n");


    for (i = 0; i <students.length; i++){


        System.out.print(students[i] + " \n");


    }


    // cycle through the each group (outside part of the array)
        for (row=0; row<scores.length; row++){

            //create variables to store computations and set initial value to zero
            int sum = 0;
            double  avg = 0;
            //System.out.print(students[i]);


            // cycle through each set of elements of each group (inside part of array)
            for (column=0; column<scores[row].length; column++){


                // for each set of elements -- add all elements in each group (int sum must be set to 0 each time)
                     sum += scores[row][column];

                     // calculate average by dividing the value in int sum by the number of elements in the group (group size)
                     avg = sum/scores[column].length;



                     // display the values of each row
                 System.out.print(scores[row][column] + "      ");


            }



        // display the sum of each group (row) with an identifying message
            System.out.print(sum + "           " );


            // display the average of each group (row) with an identifying message
           System.out.print(avg);

        //    System.out.print(" -------------------------\n");
           // create new line after each computation
            System.out.println();

            // Create dotted lines to separate each set of values displayed
            System.out.print("----------------------------------------\n");

    }
    ///////

} }

package Examples;

public class MarksQuiz {
    public static void main(String[] args) {
        // create variables to be used inside the "for" loops as counters
        int i;
        int row;
        int column;
        String[] students = { "Peter", "Lydia", "Kate", "Steve", "Alexa" };
        // Create a 2-D array of the int type to stores scores
        int[][] scores = { { 82, 90 }, { 78, 90 }, { 84, 80 }, { 85, 73 }, { 81, 93 } };

        // Display headings for information to be spaced equally
        System.out.printf("%-7s%-7s%-7s%-7s%-7s", "Name", "Test1", "Test2", "Sum of tests ", "Average grade\n");
        System.out.print("----------------------------------------\n");

        //Removed the For loop from Here
        /*for (i = 0; i < students.length; i++) {
            System.out.print(students[i] + " \n");
        }*/

        // cycle through the each group (outside part of the array)
        for (row = 0; row < scores.length; row++) {

                System.out.print(students[row]);//Inserted the Print Statement to Fix your Issue
                    // create variables to store computations and set initial value to
            // zero
            int sum = 0;
            double avg = 0;
            // System.out.print(students[i]);

            // cycle through each set of elements of each group (inside part of
            // array)
            for (column = 0; column < scores[row].length; column++) {

                // for each set of elements -- add all elements in each group
                // (int sum must be set to 0 each time)
                sum += scores[row][column];

                // calculate average by dividing the value in int sum by the
                // number of elements in the group (group size)
                avg = sum / scores[column].length;

                // display the values of each row
                System.out.print(scores[row][column] + "      ");

            }

            // display the sum of each group (row) with an identifying message
            System.out.print(sum + "           ");

            // display the average of each group (row) with an identifying
            // message
            System.out.print(avg);

            // System.out.print(" -------------------------\n");
            // create new line after each computation
            System.out.println();

            // Create dotted lines to separate each set of values displayed
            System.out.print("----------------------------------------\n");

        }
    }
}

输出:-

名称 Test1 Test2 测试总和 平均成绩

彼得 82 90 172 86.0

莉迪亚78 90 168 84.0

凯特 84 80 164 82.0

史蒂夫 85 73 158 79.0

Alexa81 93 174 87.0