编写一个学生类和教师类数据输入和显示的Java程序,学生类有编号、姓名、班号、语文成绩、英语成绩、数学成绩,教师数据有编号、姓名、职称和部门。班级有10名同学,写代码完成老师录入一个班级的学生成绩,统计输出该班级每位同学的平均成绩,以及该班级各科目的平均成绩。
时间: 2024-05-04 14:22:30 浏览: 88
编写一个学生和教师数据输入和显示程序
下面是一个简单的Java程序,实现了学生类和教师类的定义以及成绩的统计输出。
```java
import java.util.Scanner;
class Student {
int id;
String name;
int classNo;
int chineseScore;
int englishScore;
int mathScore;
public Student(int id, String name, int classNo, int chineseScore, int englishScore, int mathScore) {
this.id = id;
this.name = name;
this.classNo = classNo;
this.chineseScore = chineseScore;
this.englishScore = englishScore;
this.mathScore = mathScore;
}
}
class Teacher {
int id;
String name;
String title;
String department;
public Teacher(int id, String name, String title, String department) {
this.id = id;
this.name = name;
this.title = title;
this.department = department;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 创建教师对象
Teacher teacher = new Teacher(1, "张老师", "高级教师", "数学系");
// 创建10个学生对象
Student[] students = new Student[10];
for (int i = 0; i < 10; i++) {
System.out.println("请输入第" + (i + 1) + "个学生的信息:");
System.out.print("编号:");
int id = scanner.nextInt();
System.out.print("姓名:");
String name = scanner.next();
System.out.print("班号:");
int classNo = scanner.nextInt();
System.out.print("语文成绩:");
int chineseScore = scanner.nextInt();
System.out.print("英语成绩:");
int englishScore = scanner.nextInt();
System.out.print("数学成绩:");
int mathScore = scanner.nextInt();
students[i] = new Student(id, name, classNo, chineseScore, englishScore, mathScore);
}
// 统计每个同学的平均成绩
double[] avgScores = new double[10];
for (int i = 0; i < 10; i++) {
avgScores[i] = (students[i].chineseScore + students[i].englishScore + students[i].mathScore) / 3.0;
}
// 统计各科目的平均成绩
double avgChineseScore = 0;
double avgEnglishScore = 0;
double avgMathScore = 0;
for (int i = 0; i < 10; i++) {
avgChineseScore += students[i].chineseScore;
avgEnglishScore += students[i].englishScore;
avgMathScore += students[i].mathScore;
}
avgChineseScore /= 10;
avgEnglishScore /= 10;
avgMathScore /= 10;
// 输出结果
System.out.println("班级各科目平均成绩:");
System.out.println("语文:" + avgChineseScore);
System.out.println("英语:" + avgEnglishScore);
System.out.println("数学:" + avgMathScore);
System.out.println("各学生平均成绩:");
for (int i = 0; i < 10; i++) {
System.out.println(students[i].name + ":" + avgScores[i]);
}
}
}
```
这个程序的逻辑比较简单,首先创建一个教师对象和10个学生对象。然后通过循环输入每个学生的信息,并且计算每个学生的平均成绩以及各科目的平均成绩。最后输出统计结果。
阅读全文