java编写1、题目描述:某班有30名学生,每个学生有学号、姓名和4门成绩(学生信息随机产生,存入一个对象数组),现新转来一名学生,请输入这位学生的学号、姓名和成绩(四门课的成绩小于100分,从键盘输入,以空格为区分),然后显示这名学生平均分、最高分,全班的最高平均分学生信息、新同学在班级中的成绩水平,可能的结果如下所示:例1:请输入学生姓名: jack请输入学生学号: 13201109请输入四门课的成绩,以空格分隔: 89 78 94 0jack(ID: 13201109) 平均分 66.50 最高分 94.00rose (ID:13201104) 的最高平均分 90.12 最高分 100.00jack的平均成绩低于全班的平均分例2请输入学生姓名: jack请输入学生学号: 13201109请输入四门课的成绩,以空格分隔: 100 85 65 85jack(ID: 13201109) 平均分 72.38 最高分 100.00rose (ID:13201104) 的最高平均分 90.12 最高分 100.00jack的平均成绩高于全班的平均分例3请输入学生姓名: jack请输入学生学号: 13201109请输入四门课的成绩,以空格分隔: 110 845 100000 100课程成绩不能高于100分!!例4请输入学生姓名: jack请输入学生学号: 13201109请输入四门课的成绩,以空格分隔: 100 100 100 100jack(ID: 13201109) 平均分 100.00 最高分 100.00jack现在是班上成绩最好的学生了jack的平均成绩高于全班的平均分
时间: 2023-05-24 09:07:22 浏览: 740
基于Java_Web的在线考试系统的设计与实现.docx
5星 · 资源好评率100%
下面是Java代码实现:
import java.util.Scanner;
public class Student {
private String name; // 姓名
private String ID; // 学号
private int[] scores; // 四门成绩
public Student(String name, String ID, int[] scores) {
this.name = name;
this.ID = ID;
this.scores = scores;
}
public String getName() {
return name;
}
public String getID() {
return ID;
}
public int[] getScores() {
return scores;
}
public double getAverageScore() {
int sum = 0;
for (int score : scores) {
sum += score;
}
return (double) sum / scores.length;
}
public int getHighestScore() {
int max = scores[0];
for (int score : scores) {
if (score > max) {
max = score;
}
}
return max;
}
public static Student[] generateStudents(int num) {
String[] names = {"jack", "rose", "tom", "mary", "jim", "lucy", "bob", "kate", "david", "susan"};
Student[] students = new Student[num];
for (int i = 0; i < num; i++) {
String name = names[(int) (Math.random() * names.length)];
String ID = "13201" + (int) (Math.random() * 10000);
int[] scores = new int[4];
for (int j = 0; j < 4; j++) {
scores[j] = (int) (Math.random() * 100);
}
students[i] = new Student(name, ID, scores);
}
return students;
}
public static Student findHighestAverageScoreStudent(Student[] students) {
Student highestAverageScoreStudent = students[0];
double highestAverageScore = students[0].getAverageScore();
for (int i = 1; i < students.length; i++) {
double averageScore = students[i].getAverageScore();
if (averageScore > highestAverageScore) {
highestAverageScore = averageScore;
highestAverageScoreStudent = students[i];
}
}
return highestAverageScoreStudent;
}
public static boolean checkScoresInRange(int[] scores) {
for (int score : scores) {
if (score < 0 || score > 100) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 生成班级学生信息
Student[] students = generateStudents(30);
// 输入新转来的学生信息
System.out.print("请输入新转来的学生姓名:");
String name = scanner.next();
System.out.print("请输入新转来的学生学号:");
String ID = scanner.next();
int[] scores = new int[4];
System.out.print("请输入新转来的学生四门课成绩,以空格分隔:");
scores[0] = scanner.nextInt();
scores[1] = scanner.nextInt();
scores[2] = scanner.nextInt();
scores[3] = scanner.nextInt();
while (!checkScoresInRange(scores)) {
System.out.print("课程成绩不能高于100分,请重新输入新转来的学生四门课成绩:");
scores[0] = scanner.nextInt();
scores[1] = scanner.nextInt();
scores[2] = scanner.nextInt();
scores[3] = scanner.nextInt();
}
Student newStudent = new Student(name, ID, scores);
// 计算并显示新学生的平均分和最高分
double newStudentAverageScore = newStudent.getAverageScore();
int newStudentHighestScore = newStudent.getHighestScore();
System.out.printf("%s(ID: %s) 平均分 %.2f 最高分 %.2f\n", newStudent.getName(), newStudent.getID(),
newStudentAverageScore, (double) newStudentHighestScore);
// 计算并显示全班最高平均分学生信息
Student highestAverageScoreStudent = findHighestAverageScoreStudent(students);
double highestAverageScore = highestAverageScoreStudent.getAverageScore();
int highestAverageScoreStudentHighestScore = highestAverageScoreStudent.getHighestScore();
System.out.printf("%s(ID:%s) 的最高平均分 %.2f 最高分 %.2f\n", highestAverageScoreStudent.getName(),
highestAverageScoreStudent.getID(), highestAverageScore, (double) highestAverageScoreStudentHighestScore);
// 比较新学生与全班平均分的大小
double classAverageScore = 0;
for (Student student : students) {
classAverageScore += student.getAverageScore();
}
classAverageScore /= students.length;
if (newStudentAverageScore > classAverageScore) {
System.out.println(newStudent.getName() + "的平均成绩高于全班的平均分");
} else if (newStudentAverageScore < classAverageScore) {
System.out.println(newStudent.getName() + "的平均成绩低于全班的平均分");
} else {
System.out.println(newStudent.getName() + "的平均成绩与全班平均分相等");
}
}
}
阅读全文