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

Java 如何在程序中包含排序方法?,java,arrays,sorting,lines,Java,Arrays,Sorting,Lines,以下是该计划的要求。基本上,输出必须如下所示,同时仍遵循以下指导原则。我的问题是如何修改我的程序以满足要求#4(即编写一种方法,以1-D和2-D数组获取学生数据并打印学生记录:姓名、2个测验分数和平均值。输出字段应对齐。)我的第二个问题是如何满足要求#6(学生记录前应打印一行标题,每2名学生打印一行“------------------------” 名称1 姓名2 姓名3 姓名4 姓名5 ==================================================

以下是该计划的要求。基本上,输出必须如下所示,同时仍遵循以下指导原则。我的问题是如何修改我的程序以满足要求#4(即编写一种方法,以1-D和2-D数组获取学生数据并打印学生记录:姓名、2个测验分数和平均值。输出字段应对齐。)我的第二个问题是如何满足要求#6(学生记录前应打印一行标题,每2名学生打印一行“------------------------”


  • 名称1

  • 姓名2


  • 姓名3

  • 姓名4


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

  • 将学生姓名(实名)分配给1-D数组,并将2个测验分数分配给2-D数组

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

  • 4.编写一个方法,获取学生数据(1-D和2-D数组),并按平均字段从高到低排序学生记录。

  • 编写一个方法,获取学生数据(1-D和2-D数组)并打印学生记录:姓名、2个测验分数和平均值。输出字段应对齐
  • 6.学生记录前应打印一行标题,每两名学生应打印一行“------------------------”。

  • 以下输出是一个示例:
  • 名称Q1 Q2平均值
  • 名称1

  • 姓名2


  • 姓名3

  • 姓名4


  • 姓名5
  • =================================================================================

    这是我到目前为止所拥有的。我在排序方法和每两个学生打印一行“-----------------”方面确实有问题。

    公营班级学生成绩{

    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");
    
        }
        ///////
    
    publicstaticvoidmain(字符串[]args){
    //创建要在“for”循环中用作计数器的变量
    int i;
    int行;
    int列;
    字符串[]学生={“彼得”、“莉迪亚”、“凯特”、“史蒂夫”、“亚历克斯”};
    //创建一个int类型的二维数组来存储分数
    int[][]得分={82,90},{78,90},{84,80},{85,73},{81,93};
    //显示等间距信息的标题
    System.out.printf(“%-7s%-7s%-7s%-7s%-7s%-7s”,“名称”,
    “测试1”、“测试2”、“测试总数”、“平均分数”\n);
    System.out.print(“---------------------------------------\n”);
    对于(i=0;i
    package示例;
    公共级MarksQuiz{
    公共静态void main(字符串[]args){
    //创建要在“for”循环中用作计数器的变量
    int i;
    int行;
    int列;
    字符串[]学生={“彼得”、“莉迪亚”、“凯特”、“史蒂夫”、“亚历克斯”};
    //创建一个int类型的二维数组来存储分数
    int[][]得分={82,90},{78,90},{84,80},{85,73},{81,93};
    //显示等间距信息的标题
    System.out.printf(“%-7s%-7s%-7s%-7s%-7s%-7s%,”名称“,”测试1“,”测试2“,”测试总和“,”平均等级“\n”);
    System.out.print(“---------------------------------------\n”);
    //从这里删除For循环
    /*对于(i=0;i
    输出:-

    名称Test1 Test2测试总和平均等级 彼得829017286.0 Lydia78 90 168 84.0 Kate84 80 164 82.0 Steve85 73 15879.0
    Alexa81 93 174 87.0请清楚地缩进问题并设置其格式。我认为输出应按平均列排序。请不要为其他人做家庭作业。这不是StackOverflow的目的。
    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");
    
            }
        }
    }