java:基于控制台的学生成绩统计系统代码
时间: 2024-09-25 09:13:25 浏览: 60
java作业学生成绩管理系统(控制台实现)
5星 · 资源好评率100%
创建一个简单的基于控制台的学生成绩统计系统的Java代码示例可以包括以下几个步骤:
```java
import java.util.Scanner;
class Student {
String name;
int score;
// 构造函数
public Student(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "学生姓名: " + name + ", 成绩: " + score;
}
}
public class GradeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生的数量:");
int numStudents = scanner.nextInt();
Student[] students = new Student[numStudents];
for (int i = 0; i < numStudents; i++) {
System.out.println("请输入第" + (i+1) + "名学生的姓名和成绩:");
String name = scanner.next();
int score = scanner.nextInt();
students[i] = new Student(name, score);
}
// 统计最高分和最低分
int maxScore = Integer.MIN_VALUE;
int minScore = Integer.MAX_VALUE;
for (Student student : students) {
if (student.score > maxScore) {
maxScore = student.score;
}
if (student.score < minScore) {
minScore = student.score;
}
}
System.out.println("最高分: " + maxScore + " - " + findMaxName(students));
System.out.println("最低分: " + minScore + " - " + findMinName(students));
scanner.close();
}
private static String findMaxName(Student[] students) {
int maxIndex = 0;
for (int i = 1; i < students.length; i++) {
if (students[i].score > students[maxIndex].score) {
maxIndex = i;
}
}
return students[maxIndex].name;
}
private static String findMinName(Student[] students) {
int minIndex = 0;
for (int i = 1; i < students.length; i++) {
if (students[i].score < students[minIndex].score) {
minIndex = i;
}
}
return students[minIndex].name;
}
}
```
这个程序首先会提示用户输入学生数量、姓名和成绩,然后计算并打印出最高分和最低分,同时显示对应的学生名字。这里我们使用了数组存储学生信息,并通过遍历来查找最高分和最低分对应的姓名。
阅读全文