用java语言 编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号、语文成绩、英语成绩、数学成绩,教师数据有编号、姓名、职称和部门。班级有10名同学,写代码完成老师录入一个班级的学生成绩,统计输出该班级每位同学的平均成绩,以及该班级各科目的平均成绩。
时间: 2023-07-10 17:34:27 浏览: 179
编写一个学生和教师数据输入和显示程序
以下是用Java语言实现学生和教师数据输入和显示程序的基本代码,可以根据需要进行修改和完善。
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 输入教师信息
Teacher teacher = new Teacher();
teacher.inputInfo();
// 输入学生信息
Student[] students = new Student[10];
for (int i = 0; i < students.length; i++) {
students[i] = new Student();
students[i].inputInfo();
}
// 录入学生成绩
teacher.inputGrades(students);
// 统计并输出班级平均成绩和各科目平均成绩
double sumChinese = 0;
double sumEnglish = 0;
double sumMath = 0;
for (int i = 0; i < students.length; i++) {
sumChinese += students[i].getChinese();
sumEnglish += students[i].getEnglish();
sumMath += students[i].getMath();
}
double avgChinese = sumChinese / students.length;
double avgEnglish = sumEnglish / students.length;
double avgMath = sumMath / students.length;
System.out.println("班级平均成绩:");
System.out.println("语文:" + avgChinese);
System.out.println("英语:" + avgEnglish);
System.out.println("数学:" + avgMath);
System.out.println("每位同学的平均成绩:");
for (int i = 0; i < students.length; i++) {
double avg = students[i].getAvg();
System.out.println(students[i].getName() + ":" + avg);
}
}
}
class Teacher {
private int id;
private String name;
private String title;
private String department;
public void inputInfo() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入教师编号:");
this.id = scanner.nextInt();
System.out.println("请输入教师姓名:");
this.name = scanner.next();
System.out.println("请输入教师职称:");
this.title = scanner.next();
System.out.println("请输入教师所在部门:");
this.department = scanner.next();
}
public void inputGrades(Student[] students) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入班级学生成绩:");
for (int i = 0; i < students.length; i++) {
System.out.println("请输入" + students[i].getName() + "的语文成绩:");
double chinese = scanner.nextDouble();
students[i].setChinese(chinese);
System.out.println("请输入" + students[i].getName() + "的英语成绩:");
double english = scanner.nextDouble();
students[i].setEnglish(english);
System.out.println("请输入" + students[i].getName() + "的数学成绩:");
double math = scanner.nextDouble();
students[i].setMath(math);
}
}
}
class Student {
private int id;
private String name;
private String classNo;
private double chinese;
private double english;
private double math;
public void inputInfo() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生编号:");
this.id = scanner.nextInt();
System.out.println("请输入学生姓名:");
this.name = scanner.next();
System.out.println("请输入学生班级:");
this.classNo = scanner.next();
}
public double getAvg() {
return (chinese + english + math) / 3;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public void setEnglish(double english) {
this.english = english;
}
public void setMath(double math) {
this.math = math;
}
public String getName() {
return name;
}
public double getChinese() {
return chinese;
}
public double getEnglish() {
return english;
}
public double getMath() {
return math;
}
}
```
以上程序中,Teacher类表示教师信息,包括编号、姓名、职称和部门,可以通过inputInfo()方法进行输入。Student类表示学生信息,包括编号、姓名、班级和成绩,可以通过inputInfo()方法进行输入,可以通过setChinese()、setEnglish()和setMath()方法设置学生成绩,可以通过getAvg()方法获取学生平均成绩。Main类表示程序入口,可以通过调用Teacher和Student类的方法实现学生和教师数据输入和显示,并统计班级平均成绩和各科目平均成绩。
阅读全文