文章内容发布于 20 天前;最后修改于 20 日前。其中的信息可能发生变化或产生更改,敬请留意。
2025-12-06java作业
参照学习通资料中最新案例,应用List\
import java.util.ArrayList;
import java.util.Scanner;
public class arrayListDemo {
public static void main(String[] args) {
ArrayList<Student> stuList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
String studentClass = scanner.nextLine();
int studentNumber = Integer.parseInt(scanner.nextLine());
String studentName = scanner.nextLine();
String workAname = scanner.nextLine();
int scoreA = Integer.parseInt(scanner.nextLine());
String workBname = scanner.nextLine();
int scoreB = Integer.parseInt(scanner.nextLine());
String workCname = scanner.nextLine();
int scoreC = Integer.parseInt(scanner.nextLine());
WorkScores workScores = new WorkScores(studentNumber, scoreA, workAname, scoreB, workBname, scoreC, workCname);
Student student = new Student(studentClass, studentNumber, studentName, 0, 0, 0, workScores);
student.setMaxScore();
student.setSumScore();
student.setAverageScore();
stuList.add(student);
}
scanner.close();
//最高分
System.out.println("最高分输出");
System.out.println("姓名\t学号\t班级\t最高分");
for(int i = 0; i < stuList.size(); i++){
System.out.println(stuList.get(i).getStudentName() + "\t" + stuList.get(i).getStudentNunber() + "\t" + stuList.get(i).getStudentClass() + "\t" + stuList.get(i).getMaxScore());
}
System.out.println("平均分输出");
System.out.println("姓名\t学号\t班级\t平均分");
for(int i = 0; i < stuList.size(); i++){
System.out.println(stuList.get(i).getStudentName() + "\t" + stuList.get(i).getStudentNunber() + "\t" + stuList.get(i).getStudentClass() + "\t" + stuList.get(i).getAverageScore());
}
System.out.println("总分输出(含排序)");
System.out.println("姓名\t学号\t班级\t总分");
Student[] arr = new Student[stuList.size()];
for(int i = 0; i < stuList.size(); i++){
arr[i] = stuList.get(i);
}
for (int i = 0; i < arr.length - 1; i++){
for (int j = 0; j < arr.length - 1 - i; j++){
if(arr[j].getSumScore()>arr[j+1].getSumScore() ){
Student tempStudent = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tempStudent;
}
}
}
for(int i = 0; i < arr.length; i++){
System.out.println(arr[i].getStudentName() + "\t" + arr[i].getStudentNunber() + "\t" + arr[i].getStudentClass() + "\t" + arr[i].getSumScore());
}
}
}
class WorkScores{
private int stuNum;
private int scoreA;
private String workAname;
private int scoreB;
private String workBname;
private int scoreC;
private String workCname;
public WorkScores() {}
public WorkScores(int stuNum, int scoreA, String workAname, int scoreB, String workBname, int scoreC, String workCname) {
this.stuNum = stuNum;
this.scoreA = scoreA;
this.workAname = workAname;
this.scoreB = scoreB;
this.workBname = workBname;
this.scoreC = scoreC;
this.workCname = workCname;
}
public int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
public int getScoreA() {
return scoreA;
}
public void setScoreA(int scoreA) {
this.scoreA = scoreA;
}
public String getWorkAname() {
return workAname;
}
public void setWorkAname(String workAname) {
this.workAname = workAname;
}
public int getScoreB() {
return scoreB;
}
public void setScoreB(int scoreB) {
this.scoreB = scoreB;
}
public String getWorkBname() {
return workBname;
}
public void setWorkBname(String workBname) {
this.workBname = workBname;
}
public int getScoreC() {
return scoreC;
}
public void setScoreC(int scoreC) {
this.scoreC = scoreC;
}
public String getWorkCname() {
return workCname;
}
public void setWorkCname(String workCname) {
this.workCname = workCname;
}
}
class Student{
private String StudentClass;
private int StudentNunber;
private String StudentName;
private int MaxScore;
private int SumScore;
private int AverageScore;
private WorkScores StudentScore;
public Student(String StudentClass, int StudentNunber, String StudentName, int MaxScore, int SumScore, int AverageScore, WorkScores StudentScore) {
this.StudentClass = StudentClass;
this.StudentNunber = StudentNunber;
this.StudentName = StudentName;
this.MaxScore = MaxScore;
this.SumScore = SumScore;
this.AverageScore = AverageScore;
this.StudentScore = StudentScore;
}
public int getMaxScore() {
return MaxScore;
}
public void setMaxScore(int MaxScore) {
this.MaxScore = MaxScore;
}
public void setMaxScore() {
if (this.StudentScore != null) {
int scoreA = this.StudentScore.getScoreA();
int scoreB = this.StudentScore.getScoreB();
int scoreC = this.StudentScore.getScoreC();
this.MaxScore = Math.max(scoreA, Math.max(scoreB, scoreC));
} else {
this.MaxScore = 0;
}
}
public int getSumScore() {
return SumScore;
}
public void setSumScore(int SumScore) {
this.SumScore = SumScore;
}
public void setSumScore() {
if (this.StudentScore != null) {
this.SumScore = this.StudentScore.getScoreA() + this.StudentScore.getScoreB() + this.StudentScore.getScoreC();
}
else {
this.SumScore = 0;
}
}
public int getAverageScore() {
return AverageScore;
}
public void setAverageScore(int AverageScore) {
this.AverageScore = AverageScore;
}
public void setAverageScore() {
if (this.StudentScore != null) {
this.AverageScore = (int)((this.StudentScore.getScoreA() + this.StudentScore.getScoreB() + this.StudentScore.getScoreC())/3);
}
else {
this.AverageScore = 0;
}
}
public WorkScores getStudentScore() {
return StudentScore;
}
public void setStudentScore(WorkScores StudentScore) {
this.StudentScore = StudentScore;
}
public String getStudentClass() {
return StudentClass;
}
public void setStudentClass(String StudentClass) {
this.StudentClass = StudentClass;
}
public int getStudentNunber() {
return StudentNunber;
}
public void setStudentNunber(int StudentNunber) {
this.StudentNunber = StudentNunber;
}
public String getStudentName() {
return StudentName;
}
public void setStudentName(String StudentName) {
this.StudentName = StudentName;
}
}
输入:
输入直接一口气丢到控制台就完事了,预期输入大致如下
2023级1班
1001
张三
语文
85
数学
92
英语
78
2023级1班
1002
李四
语文
76
数学
88
英语
94
2023级2班
2001
王五
语文
91
数学
85
英语
89
2023级2班
2002
赵六
语文
82
数学
79
英语
81
2023级3班
3001
钱七
语文
95
数学
98
英语
96
