java actual combat 1: student performance ranking problem

java actual combat 1: student performance ranking problem

Requirement :
Count the final exam results of n students in the class and rank them according to the total score. Count the test scores of m courses, input the name, student number and test scores of each student from the keyboard, calculate and sort the total scores, and output each person's ranking and total score.

code show as below:

import java.util.Scanner;
public class finalTest {
    
    
    public static void main(String[] args){
    
    
        Scanner reader=new Scanner(System.in);
        System.out.println("请输入学生总数");
        int n;
        n=reader.nextInt();
        System.out.println("请输入学生科目数");
        int m;
        m=reader.nextInt();
        Student[] stu=new Student[n];//创建学生类型的数组
        for(int i=0;i<n;i++){
    
    
            stu[i]=new Student(m);//创建对象
            System.out.println("请输入第"+(i+1)+"名学生的姓名");
            stu[i].name=reader.next();//输入字符串
            System.out.println("请输入第"+(i+1)+"名学生的学号");
            stu[i].number=reader.next();
            for(int j=0;j<m;j++){
    
    
                System.out.println("请输入第"+(i+1)+"名学生的第"+(j+1)+"科成绩");
                stu[i].Grade[j]=reader.nextInt();
            }
        }
        Calculate c=new Calculate();
        c.getSort(stu,n,m);
    }
}
class Student{
    
    
    String name;
    int[] Grade;
    String number;
    int sum;
    Student(int m) {
    
    
        this.Grade=new int[m];
    }
    public void setName(String name){
    
    
        this.name=name;
    }
    public void setNumber(String number){
    
    
        this.number=number;
    }
    public int getSum(int m) {
    
    
        for (int i = 0; i < m; i++) {
    
    
            this.sum += Grade[i];
        }
        return this.sum;
    }
}
class Calculate {
    
    
    int i;
    public void getSort(Student[] s,int n,int m) {
    
    
        Student t;
        for (this.i = 1; i < n; i++) {
    
    
            for (int j = 0; j < n-1; j++) {
    
    
                if (s[j].sum < s[j + 1].sum) {
    
    
                    t = s[j];
                    s[j] = s[j + 1];
                    s[j + 1] = t;
                }
            }
        }
        System.out.println("    姓名       学号       总分  排名");
        for (int j = 0; j < n; j++) {
    
    
            System.out.printf("%6s %12s %6d %3s",s[j].name,s[j].number,s[j].getSum(m),(j+1));
            System.out.println("");
        }
    }
}
/*
3
2
怡宝
2220191906
100
100
何妍
2220191907
99
99
赵今今
2220191908
98
98
*/

operation result:
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46020391/article/details/112273308