编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号、语文成绩、英语成绩、数学成绩,教师数据有编号、姓名、职称和部门。写一个学生类Student,老师类Teacher,老师录入学生成绩,统计输出该同学的平均成绩。用jjava写
时间: 2024-04-04 11:34:16 浏览: 61
编写一个学生和教师数据输入和显示程序
以下是一个简单的Java程序,实现了学生和教师数据的输入和显示,以及计算每个学生的平均成绩:
```
import java.util.Scanner;
class Student {
private int no;
private String name;
private int grade;
private int chinese;
private int english;
private int math;
public Student(int no, String name, int grade) {
this.no = no;
this.name = name;
this.grade = grade;
this.chinese = 0;
this.english = 0;
this.math = 0;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public void setEnglish(int english) {
this.english = english;
}
public void setMath(int math) {
this.math = math;
}
public double getAverage() {
return (double)(chinese + english + math) / 3;
}
public String toString() {
return "学生编号:" + no + ",姓名:" + name + ",班级:" + grade + ",语文成绩:" + chinese + ",英语成绩:" + english + ",数学成绩:" + math + ",平均成绩:" + getAverage();
}
}
class Teacher {
private int no;
private String name;
private String title;
private String department;
public Teacher(int no, String name, String title, String department) {
this.no = no;
this.name = name;
this.title = title;
this.department = department;
}
public void inputScore(Student student) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生 " + student.toString() + " 的语文成绩:");
int chineseScore = scanner.nextInt();
student.setChinese(chineseScore);
System.out.println("请输入学生 " + student.toString() + " 的英语成绩:");
int englishScore = scanner.nextInt();
student.setEnglish(englishScore);
System.out.println("请输入学生 " + student.toString() + " 的数学成绩:");
int mathScore = scanner.nextInt();
student.setMath(mathScore);
}
public String toString() {
return "教师编号:" + no + ",姓名:" + name + ",职称:" + title + ",部门:" + department;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student(1, "张三", 1);
students[1] = new Student(2, "李四", 1);
students[2] = new Student(3, "王五", 2);
Teacher teacher = new Teacher(1, "张老师", "教授", "计算机科学与技术");
for (int i = 0; i < students.length; i++) {
teacher.inputScore(students[i]);
}
for (int i = 0; i < students.length; i++) {
System.out.println(students[i].toString());
}
}
}
```
程序中定义了一个Student类和一个Teacher类,分别表示学生和教师的数据结构,并提供了相应的方法。在主函数中,首先创建了3个学生对象和1个教师对象,然后通过教师对象的inputScore方法,输入每个学生的成绩。最后,通过循环遍历每个学生,输出其数据和平均成绩。
阅读全文