生活随笔
收集整理的這篇文章主要介紹了
学生成绩排名问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
學生成績排名問題
要求:
統(tǒng)計班級n名同學的期末考試成績,按總成績高低進行排名。統(tǒng)計m門課程考試成績,從鍵盤輸入每個同學的姓名、學號及各門考試成績,計算總成績并排序后,輸出每人的排名及總成績。
代碼如下:
法一:2021.1.6
import java
.util
.Scanner
;
public class finalTest {public static void main(String[] args){Scanner reader
=new Scanner(System
.in
);System
.out
.println("請輸入學生總數(shù)");int n
;n
=reader
.nextInt();System
.out
.println("請輸入學生科目數(shù)");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("");}}
}
運行結(jié)果:
法二://2021.3.17
import java.util.Scanner;
public class two
{public static void main(String[] args
) {Student[] stu
=new Student[4];Scanner reader
=new Scanner(System.in
);for(int i
=0;i
<4;i
++) {stu
[i
]=new Student();for(int j
=0;j
<3;j
++) {stu
[i
].grade
[j
]=reader
.nextInt();stu
[i
].sum
+=stu
[i
].grade
[j
];stu
[i
].rank
=i
+1;}}paixu p
=new paixu();p
.paixu(stu
);p
.print(stu
);}}
class Student{int[] grade
=new int[3];int rank
;int sum
=0;
}class paixu
{int t2
;public void paixu(Student[] stu
) {for(int i
=0;i
<3;i
++) {for(int j
=0;j
<3;j
++)if(stu
[j
].sum
<stu
[j
+1].sum
) {t2
=stu
[j
].sum
;stu
[j
].sum
=stu
[j
+1].sum
;stu
[j
+1].sum
=t2
;}}}public void print(Student[] stu
) {for(int i
=0;i
<4;i
++) {stu
[i
].rank
=i
+1;System.out
.printf("NO:%d sum:%5d rank:%d\n",i
+1,stu
[i
].sum
,stu
[i
].rank
);}}
}
法三://2022.4.2
package lanqiao2022;
import java.util.Scanner;
public class six
{public static void main(String[] args
) {Scanner reader
=new Scanner(System.in
);int n
=reader
.nextInt();int m
=reader
.nextInt();Student[] stu
=new Student[n
];for(int i
=0;i
<n
;i
++) {stu
[i
]=new Student();stu
[i
].name
=reader
.next();stu
[i
].no
=reader
.next();for(int j
=0;j
<m
;j
++) {stu
[i
].grade
+=reader
.nextInt();}}System.out
.println(" name "+" no "+" grade "+"rank ");for(int i
=0;i
<n
;i
++) {System.out
.printf("%6s %12s %6d %3d",stu
[i
].name
,stu
[i
].no
,stu
[i
].grade
,(i
+1));System.out
.println();}}public void sort(Student[] stu
) {Student temp
;for(int i
=0;i
<stu
.length
;i
++) {for(int j
=0;j
<stu
.length
-1;j
++) {if(stu
[j
].grade
<stu
[j
+1].grade
) {temp
=stu
[j
+1];stu
[j
+1]=stu
[j
];stu
[j
]=temp
;}}}}
}
class Student{String name
="";String no
="";int grade
;
}
運行結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的学生成绩排名问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。