(1)定义一个结构体数组,存放10个学生的学号,姓名,三门课的成绩; 急求🙏

、定义一个结构体数组,存放 10 个学生的学号,姓名,三门课的成绩 2、从键盘输入 10 个学生的以上内容,存入文件 stud.dat,关闭文件 3、打开 stud.dat 文件,将数据读出,查看是否正确写入,关闭文件。 4、打开文件 stud.dat 文件,读出数据,将 10 个学生按照平均分数从高到低进 行排序,分别将结果输出到屏幕上和另一文件 studsort.dat 中。 5、从 studsort.dat 文件中读取第 2,4,6,8,10 个学生的数据。

之前写过一个记录学生学号,年龄,体重,名字的数组结构,你可以稍作修改就可以了,请命名文件为HW1.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Student implements Comparable<Student>, Serializable{
    /**
 * Serializable UID: ensures serialize/de-serialize process will be successful.
 */
private static final long serialVersionUID = -3515442620523776933L;

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public double getWeight() {
return weight;
}

public void setWeight(double weight) {
this.weight = weight;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

private int number;
    private int age;
    private double weight;
    private String name;
    
    public Student(int number, int age, double weight, String name) {
        this.number = number;
        this.age = age;
        this.weight = weight;
        this.name = name;
    }

@Override
public int compareTo(Student o) {
if (this.age == o.age) {
return (int)(this.weight - o.weight);
}
return this.age - o.age;
}
}

class StudentSortByAge implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
}

class StudentSortByWeight implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {
return (int)(o1.getWeight() - o2.getWeight());
}
}

public class HW1 {
//passing one and only one instance of scanner instance reduce complexity of program.
    public static void main(String[] args) {
        System.out.println("\nWelcome to the System, Choose options below: ");
     printPrompt();
    
     Student[] students = null;
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextInt()) {
            switch (scanner.nextInt()) {
                case 1:
                 System.out.println("Print Student Information");
                 if (students == null) {
                 System.out.println("Please Initilise N students first");
                 } else {
                 printStudents(students);
                 }
                 printPrompt();
                    break;
                case 2:
                    System.out.println("Enter numebr of students you want to create: ");
                    int number = scanner.nextInt();
                    students = initilise(number, scanner);
                    printPrompt();
                    break;
                case 3:
                 System.out.println("Add a new student");
                 printPrompt();
                 if (students == null) {
                 System.out.println("Please Initilise N students first");
                 } else {
                 int newLength = students.length + 1;
                 students = Arrays.copyOf(students, newLength);
                 students[newLength - 1] = createStudent(scanner);
                 System.out.println("New student has been added.");
                 printPrompt();
                 }
                    break;
                case 4:
                 System.out.println("Sorting by Age, Weight in Asce order");
                 if (students == null) {
                 System.out.println("Please Initilise N students first");
                 } else {
                 Student[] sortedStudents = students;
                 Arrays.sort(sortedStudents);
                 printStudents(sortedStudents);
                 }
                 break;
                case 5:
                 System.out.println("Calcaulte Min, Max, Ave of Age and Weight");
                 if (students == null) {
                 System.out.println("Please Initilise N students first");
                 } else {
                 Student[] sortedAgeStudents = students;
                 Arrays.sort(sortedAgeStudents,  new StudentSortByAge());
                 Student[] sortedWeightStudents = students;
                 Arrays.sort(sortedWeightStudents,  new StudentSortByWeight());
                 int averageAge = 0;
                 double averageWeight = 0.0;
                 for (Student student : sortedAgeStudents) {
                 averageAge += student.getAge();
                 averageWeight += student.getWeight();
                 }
                 averageAge = averageAge / sortedAgeStudents.length;
                 averageWeight = averageWeight / sortedWeightStudents.length;
                 System.out.printf("Min Age: %d, Max Age: %d, Ave Age: %d\n", sortedAgeStudents[0].getAge(), sortedAgeStudents[sortedAgeStudents.length - 1].getAge(), averageAge);
                 System.out.printf("Min Weight: %f, Max Weight: %f, Ave Weight: %f\n", sortedWeightStudents[0].getWeight(), sortedWeightStudents[sortedWeightStudents.length - 1].getWeight(), averageWeight);
                 }
                 break;
                case 6:
                 System.out.println("Write Student data into file");
                 try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("studentsData"), true))) {
                     oos.writeObject(students);
                     printPrompt();
                 } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
                 break;
                case 7:
                 System.out.println("Read studentData from file");
                 try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("studentsData")))) {
                 students = (Student[]) (ois.readObject());
                 printPrompt();
                 } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
                 break;
                default:
                 scanner.close();
                 System.out.println("Quit");
                    break;
            }
        }
    }

    public static void printPrompt() {
        System.out.println("1: Display current students");
        System.out.println("2: Initilise N students");
        System.out.println("3: Add new student");
        System.out.println("4: Sorting by Age, Weight in Asce order");
        System.out.println("5: Calcaulte Min, Max, Ave of Age and Weight");
        System.out.println("6: Write Student data into file");
        System.out.println("7: Read studentData from file");
    }
    
    public static Student[] initilise(int n, Scanner scanner) {
        Student[] students = new Student[n];
        for (int i = 0; i < n; i ++) {
            students[i] = createStudent(scanner);
        }
        System.out.println("Initilisation of students complete.");
        return students;
    }

    public static void printStudents(Student[] students) {
     for (Student student : students) {
     System.out.printf("Student: %s, Number: %d, Age: %d, Weight: %f\n", student.getName(), student.getNumber(), student.getAge(), student.getWeight());
     }
    }

    public static void printSortedStudents(Student[] students) {
     for (Student student : students) {
     System.out.printf("Age: %d, Weight: %f, Student: %s, Number: %d\n", student.getAge(), student.getWeight(), student.getName(), student.getNumber());
     }
    }

    public static Student createStudent(Scanner scanner) {
        int studentNumber = 0, studentAge = 0;
        double studentWeight = 0.0;
        String studentName = null;
        System.out.println("Enter Student Number: ");
        while(scanner.hasNext()) {
            studentNumber = scanner.nextInt();
            
            System.out.println("Enter Student Age: ");
            studentAge = scanner.nextInt();
            
            System.out.println("Enter Student Weight: ");
            //nextDouble仅仅读取double的值,在double值后的'\n'将会被nextLine()所读取,所以读取studentName时需要再读取一次nextLine()
            studentWeight = scanner.nextDouble();
            
            System.out.println("Enter Student Name: ");
            scanner.nextLine();
            studentName = scanner.nextLine();
            break;
        }
        return new Student(studentNumber, studentAge, studentWeight, studentName);
    }
}

运行结果,

温馨提示:答案为网友推荐,仅供参考
相似回答