首页 > 解决方案 > 需要帮助来使用数组对象显示数组的所有数据(不能使用数组列表)

问题描述

我正在创建一个学校系统,其中显示所有班级和学生列表以及他们的成绩和总成绩**。

但现在我真的被困住了。我可以在课堂上显示所有数据的列表,但我现在真的不知道该怎么办。请帮我处理我的代码。

在这段代码中,我使用Array 来存储学生、年级、班级名称和班级数量的数据。

班级和学生的阵列:-

班级最多可存储 [10 个班级],但初始时最少 3 个班级,最多不超过 10 名学生,最多可存储 [20 名学生],最多不超过 20 名

此代码中不得使用任何阵列列表。

= 我想要的学校列表的输出 =

在此输出中,名称按等级升序排列,并在顶部按数字列出,其中显示了总等级最高的班级。用户询问是否返回功能选择

=== SCHOOL LISTS ===

1. Class Thunder with Total grade: 146
2. Class Lightning with Total grade: 103.9
3. Class Sunrise with Total grade: 115.5

--------------------
Class: Sunrise
Class quantity: 5

Student1: James Shawn
Grade: 75
Student2: Ali Pole
Grade: 30.5
Student3: Tong Kim
Grade: 10

Total Grade: 115.5
------------------------
Class: Thunder
Class quantity: 5

Student1: Mark Lee
Grade: 65.5
Student2: James Mic 
Grade: 20.4
Student3: James Mic 
Grade: 18.0

Total Grade: 103.9
------------------------
Class: Lightning 
Class quantity: 5

Student1: Luke Kim
Grade: 90
Student2: Noth Shawn
Grade: 44
Student3: Lex Wale
Grade: 12

Total Grade: 146

Do you want to go back to the selection function?
Press Y or N: 
//if yes they will go back to the functionSelection
//if no they will ask users if they want to exit the systems.

Do you want to exit (Y or N): 

//if yes they will exit the systems
//if no they will go back to the functionSelection.

我的 JAVA 课程 学生班:-

import java.util.Comparator;

public class Student {

    private String studentName;
    private int grade;

    public Student(String studentName, int score) {
        this.studentName = studentName;
        this.grade = grade;
    }

    public String toString() {
        return "\nStudent Name: " + studentName + "\nGrade: " + grade;
    }

    public String getStudentName() {
        return studentName;
    }

    public int getGrade() {
        return grade;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

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

//Sorting side
class sortGrade implements Comparator<Student> {

        @Override
        public int compare(Student a, Student b) {
        return (a.getGrade() - b.getGrade());
    }
}

班级班级:-

public class Class {

    Student[] student;
    private String className;
    private int classQuantity;
    private int totalGrade = 0;
    
    //add student to the class
     public void addNewStudent(String studentName, double grade) {

    }
    
    public double calculateTotalGrade() {

        for (Student students : student) {
            totalGrade += students.getGrade();
        }
        return Math.round(totalGrade * 10.0) / 10.0;
    }

    public Class(Student[] student,
            String className, int classQuantity) {

        this.student = student;
        this.className = className;
        this.classQuantity = classQuantity;
    }

    public Class(String className, int classQuantity) {

        this.className = className;
        this.classQuantity = classQuantity;
    }

    public String toString() {
        String studentList = " ";

        for (int i = 0; i < student.length; i++) {
            studentList += "\n\tStudent " + (i + 1) + ": " + student[i].getStudentName()
                    + "\n\tGrade: " + student[i].getGrade();
        }

        return "\nClass Name: " + className
                + "\nClass Quantity: " + classQuantity + "\n" + studentList;
    }
}

主驾驶课程:-

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class MainDriver {

    private Object[] a;

    void printSort(Student[] sortList) {
        for (Object sort : a) {
            System.out.print(sort + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {

        int size = 10;
        Class[] classroom = new Class[size];

        Student[] studentList1 = {
            new Student("Tong Kim", 10),
            new Student("Ali Pole", 30),
            new Student("James Shawn", 75)};

        Student[] studentList2 = {
            new Student("James Mic", 20),
            new Student("Ho Kim", 18),
            new Student("Mark Lee", 65)};

        Student[] studentList3 = {
            new Student("Luke Kim", 90),
             new Student("Noth Shawn", 44),
             new Student("Lex Wale", 12)
        };

        classroom[0] = new Class(studentList1, "Sunrise", 20);
        classroom[1] = new Class(studentList2, "Thunder", 10);
        classroom[2] = new Class(studentList3, "Lightning", 5);

        Scanner input = new Scanner(System.in);
        boolean functionSelection = true;
        
       // Student[] SortList = studentList1;

        while (functionSelection) {

            System.out.println("=== SCHOOL SYSTEM === \n");
            System.out.println("[1] Add New Class");
            System.out.println("[2] Exit");

            System.out.print("\nChoose a function: ");

            //Check if it is other then number
            if (!input.hasNextInt()) {
                System.out.println(" \nChoose the existing number");
                input.nextLine();
                continue;
            }

            int choice = input.nextInt();

            String newClassName, searchClass, newName, addName;
            switch (choice) {
                case 1:
                  
                    //Show School List
                    /*List all class, student, grade and the total grade
                    in ascending*/

                    System.out.println("=== SCHOOL LIST ===");

                   Student[] SortList = studentList1;

                   //Descending Order Arrays
         
                    Comparator<Student> descendingOrder;
                    descendingOrder = Collections.reverseOrder
                    (new sortGrade());

                    Arrays.sort(SortList, descendingOrder);
                    System.out.println(Arrays.toString(SortList) + "\n");
                case 2: 
                    System.exit(0);
                    break;
                default:
                    System.out.println(" \nChoose the existing number");
            }
        }
    }
}

标签: javaarrayssortingconstructorcomparator

解决方案


如果您绝对不能使用 ArrayList,那么您唯一的其他选择是重新初始化数组,然后将新数组分配给原始指针。您可以将此功能封装到这样的方法中:

String[] push(String[] array, String push) {
    String[] longer = new String[array.length + 1];
    for (int i = 0; i < array.length; i++)
        longer[i] = array[i];
    longer[array.length] = push;
    return longer;
}

当您调用此方法时,您必须将其返回值分配给原始数组值。不过,这可能会产生副作用。所以你应该小心它。使用较大的数组可能会很麻烦,因为每次添加内容时都必须对其进行迭代。


推荐阅读