首页 > 解决方案 > 通过并行数组对学生记录进行排序

问题描述

有人可以帮我整理学生记录吗?问题是,程序应该按字典顺序排序,但其他元素保持不变。

样品运行:

How many student records will be created?2
Enter the id number of student 1:321
Enter the name of student 1:mika
Enter the course of student 1:CS
Enter the year level of student 1:1
Enter the prelim grade of student 1:90
Enter the midterm grade of student 1:98
Enter the tentative final grade of student 1:90
Enter the id number of student 2:432
Enter the name of student 2:kyle
Enter the course of student 2:IT
Enter the year level of student 2:1
Enter the prelim grade of student 2:97
Enter the midterm grade of student 2:89
Enter the tentative final grade of student 2:88

原始订购数据:

id number Name                     course    year PG    MG    TFG   FG    
321       mika                     CS        1    90    98    90    93    
432       kyle                     IT        1    97    89    88    91 

预期输出(排序)

id number Name                     course    year PG    MG    TFG   FG    
432       kyle                     IT        1    97    89    88    91    
321       mika                     CS        1    90    98    90    93    

这是我的代码:

import java.util.*;

public class StudentRecordsViaParallelArrays {

    static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args){

        String[] id; // Array that will store ID numbers
        String[] names; // Array that will store names
        String[] course;// Array that will store courses
        int[] yearLevel; // array that will store year levels
        int count; // variable that will store the number of students
        byte[] pGrade; // to store prelim grade
        byte[] mGrade; // to store midterm grade
        byte[] tFGrade; // to store tentative Final Grade
        byte[] fGrade ; // to store final grade

            System.out.print("How many student records will be created?");
            count = Integer.parseInt(keyboard.nextLine());

            // Instantiate the arrays such that each will have a length=size
            id = new String[count];
            names = new String[count];
            course = new String[count];
            yearLevel = new int[count];
            pGrade = new byte[count];
            mGrade = new byte[count];
            tFGrade = new byte[count];
            fGrade = new byte[count];

        //Invoke the method that will fill the arrays with elements
        populateArrays(id, names, course, yearLevel, pGrade, mGrade, tFGrade, fGrade);
        // Show the students following the order by which they were entered
        System.out.println("Original Order Data");
        // Invoke the method for displaying the array elements
        showData(id,names,course,yearLevel, pGrade, mGrade, tFGrade, fGrade);
        // Invoke the method that will sort the arrays in parallel
        System.out.println("Sorted Data");
        sortDataBasedOnNames2(id, names, course, yearLevel, pGrade, mGrade, tFGrade, fGrade,count);// Invoke the method for
        // displaying the array elements
        // Show the students in sorted order

        //showData(id,names,course,yearLevel, pGrade, mGrade, tFGrade, fGrade);

    }//end of main method

    /**
     *Put elements into arrays (parallel arrays) for ID numbers, names, course, year levels, prelim grade,
     * midterm grade, tentative final grade, and final grade
     **/
    public static void populateArrays(String[] id, String[] n, String[] c, int[] y, byte[] p, byte[] m, byte[] t, byte[] f) {
        for (int index = 0; index < n.length; index++) {
            System.out.print("Enter the id number of student " + (index + 1) + ":");
            id[index] = keyboard.nextLine();
            System.out.print("Enter the name of student " + (index + 1) + ":");
            n[index] = keyboard.nextLine();
            System.out.print("Enter the course of student " + (index + 1) + ":");
            c[index] = keyboard.nextLine();
            System.out.print("Enter the year level of student " + (index + 1) + ":");
            y[index] = Integer.parseInt(keyboard.nextLine());
            System.out.print("Enter the prelim grade of student " + (index + 1) + ":");
            p[index] = Byte.parseByte(keyboard.nextLine());
            System.out.print("Enter the midterm grade of student " + (index + 1) + ":");
            m[index] = Byte.parseByte(keyboard.nextLine());
            System.out.print("Enter the tentative final grade of student " + (index + 1) + ":");
            t[index] = Byte.parseByte(keyboard.nextLine());

            // compute the final grade of student as the average of prelim, midterm and tentative final grade
            f[index] = (byte) ((1.0 * p[index] + 1.0 * m[index] + 1.0 * t[index]) / 3.0 + 0.5);
        }
        return;
    }//end of populateArrays method

    /**
     Sort the elements of the eight arrays in parallel (i.e. When
     there is a need to swap elements of the array n,
     the corresponding elements in other arrays should also
     be swapped.) such that the elements of array n
     are lexicographically arranged. (SPECIFY the Apply a Sort Algorithm
     that you will follow*/
    public static void sortDataBasedOnNames2(String[] id, String[] n, String[] c, int[] yLevel, byte[] p, byte[] m, byte[] t,
                                            byte[] f, int ctr){

        for(int i = 0; i<ctr-1; i++){
            for (int j = i+1; j<n.length; j++){
                //compares each elements of the array to all the remaining elements
                if(n[i].compareTo(n[j])>0){
                    //swapping array elements
                    String temp = n[i];
                    n[i] = n[j];
                    n[j] = temp;
                } // end of if-statement
            } // end of second for loop
        }  // end of first for loop


        //System.out.println("Alphabetical Order:");
        System.out.printf("%-10s%-25s%-10s%-5s%-6s%-6s%-6s%-6s%n","id number","Name", "course", "year", "PG",
                "MG", "TFG", "FG");
        for (int i = 0; i <= ctr - 1; i++){
            System.out.printf("%-10s%-25s%-10s%-5d%-6d%-6d%-6d%-6d%n",id[i],n[i], c[i], yLevel[i],p[i],m[i],t[i],f[i]);
        }//end of for loop

    } // end of sortBasedOnNames method

    /**
     *Show the elements of the arrays on the screen.
     **/
    public static void showData(String[] id, String[] n, String[] c, int[] y, byte[] p, byte[] m, byte[] t, byte[] f){
        System.out.printf("%-10s%-25s%-10s%-5s%-6s%-6s%-6s%-6s%n","id number","Name", "course", "year", "PG",
                "MG", "TFG", "FG");
        for (int x=0; x < n.length; x++)
            System.out.printf("%-10s%-25s%-10s%-5d%-6d%-6d%-6d%-6d%n",id[x],n[x], c[x], y[x],p[x],m[x],t[x],f[x]);
    } // end of showData method

}//end of class

我怎样才能达到这个要求?我尝试过这种方式。有更好的建议吗?

标签: javaarrayssorting

解决方案


推荐阅读