java7. 完成一个学生管理系统,定义学生类,输入3-5个学生的姓名和成绩,计算学生的平均成绩,所有的功能在学生类中完成,在Demo类中定义main函数演示功能;
时间: 2024-05-09 09:20:47 浏览: 66
学生类:
```java
public class Student {
private String name;
private double score;
public Student(String name, double score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public double getScore() {
return score;
}
}
```
Demo类:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<Student>();
// 输入学生信息
System.out.println("请输入学生信息:");
for (int i = 0; i < 5; i++) {
System.out.print("请输入第" + (i + 1) + "个学生的姓名:");
String name = scanner.next();
System.out.print("请输入第" + (i + 1) + "个学生的成绩:");
double score = scanner.nextDouble();
students.add(new Student(name, score));
}
// 计算平均成绩
double sum = 0;
for (Student student : students) {
sum += student.getScore();
}
double avg = sum / students.size();
// 输出学生信息和平均成绩
System.out.println("学生信息如下:");
for (Student student : students) {
System.out.println("姓名:" + student.getName() + ",成绩:" + student.getScore());
}
System.out.println("平均成绩为:" + avg);
}
}
```
阅读全文