用Java编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号、语文成绩、英语成绩、数学成绩,教师数据有编号、姓名、职称和部门。写一个学生类Student,老师类Teacher,老师录入学生成绩,统计输出该同学的平均成绩。
时间: 2024-04-04 21:35:00 浏览: 60
java代码(输入学生成绩,并大小排列
5星 · 资源好评率100%
好的,我来帮你解答这个问题。
首先,我们需要先定义两个类:Student和Teacher。
```java
public class Student {
private String id;
private String name;
private String classNum;
private double chineseScore;
private double englishScore;
private double mathScore;
// 构造函数
public Student(String id, String name, String classNum, double chineseScore, double englishScore, double mathScore) {
this.id = id;
this.name = name;
this.classNum = classNum;
this.chineseScore = chineseScore;
this.englishScore = englishScore;
this.mathScore = mathScore;
}
// 计算平均分
public double calculateAverageScore() {
return (chineseScore + englishScore + mathScore) / 3;
}
// getter方法
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getClassNum() {
return classNum;
}
public double getChineseScore() {
return chineseScore;
}
public double getEnglishScore() {
return englishScore;
}
public double getMathScore() {
return mathScore;
}
}
public class Teacher {
private String id;
private String name;
private String title;
private String department;
// 构造函数
public Teacher(String id, String name, String title, String department) {
this.id = id;
this.name = name;
this.title = title;
this.department = department;
}
// 录入学生成绩
public void inputStudentScore(Student student, double chineseScore, double englishScore, double mathScore) {
student.chineseScore = chineseScore;
student.englishScore = englishScore;
student.mathScore = mathScore;
}
// getter方法
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getTitle() {
return title;
}
public String getDepartment() {
return department;
}
}
```
然后,我们可以编写一个测试类来测试我们的代码。
```java
public class Test {
public static void main(String[] args) {
// 创建教师对象
Teacher teacher = new Teacher("001", "张老师", "教授", "计算机科学与技术系");
// 创建学生对象
Student student = new Student("180001", "小明", "18软件1班", 0, 0, 0);
// 教师录入学生成绩
teacher.inputStudentScore(student, 90, 80, 85);
// 输出该同学的平均成绩
System.out.println("学生姓名:" + student.getName());
System.out.println("学生平均成绩:" + student.calculateAverageScore());
}
}
```
输出结果:
```
学生姓名:小明
学生平均成绩:85.0
```
这样,我们就完成了这个程序的编写。
阅读全文