编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班号、语文成绩、英语成绩、数学成绩,教师数据有编号、姓名、职称和部门。班级有10名同学,写代码完成老师录入一个班级的学生成绩,统计输出该班级每位同学的平均成绩,以及该班级各科目的平均成绩。

时间: 2024-04-04 18:35:03 浏览: 28
好的,我来帮你解答这个问题。 首先,我们需要先定义两个类: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) { this.id = id; this.name = name; this.classNum = classNum; } // 录入语文成绩 public void inputChineseScore(double chineseScore) { this.chineseScore = chineseScore; } // 录入英语成绩 public void inputEnglishScore(double englishScore) { this.englishScore = englishScore; } // 录入数学成绩 public void inputMathScore(double mathScore) { 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.inputChineseScore(chineseScore); student.inputEnglishScore(englishScore); student.inputMathScore(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[] students = new Student[10]; students[0] = new Student("180001", "小明", "18软件1班"); students[1] = new Student("180002", "小红", "18软件1班"); students[2] = new Student("180003", "小刚", "18软件1班"); students[3] = new Student("180004", "小李", "18软件1班"); students[4] = new Student("180005", "小张", "18软件1班"); students[5] = new Student("180006", "小王", "18软件1班"); students[6] = new Student("180007", "小陈", "18软件1班"); students[7] = new Student("180008", "小杨", "18软件1班"); students[8] = new Student("180009", "小胡", "18软件1班"); students[9] = new Student("180010", "小林", "18软件1班"); // 教师录入学生成绩 teacher.inputStudentScore(students[0], 90, 80, 85); teacher.inputStudentScore(students[1], 85, 90, 80); teacher.inputStudentScore(students[2], 80, 85, 90); teacher.inputStudentScore(students[3], 90, 90, 90); teacher.inputStudentScore(students[4], 85, 85, 85); teacher.inputStudentScore(students[5], 80, 80, 80); teacher.inputStudentScore(students[6], 90, 80, 85); teacher.inputStudentScore(students[7], 85, 90, 80); teacher.inputStudentScore(students[8], 80, 85, 90); teacher.inputStudentScore(students[9], 90, 90, 90); // 统计输出该班级每位同学的平均成绩 System.out.println("该班级每位同学的平均成绩:"); for (Student student : students) { System.out.println(student.getName() + ":" + student.calculateAverageScore()); } // 统计输出该班级各科目的平均成绩 double chineseScoreSum = 0; double englishScoreSum = 0; double mathScoreSum = 0; for (Student student : students) { chineseScoreSum += student.getChineseScore(); englishScoreSum += student.getEnglishScore(); mathScoreSum += student.getMathScore(); } double chineseScoreAverage = chineseScoreSum / students.length; double englishScoreAverage = englishScoreSum / students.length; double mathScoreAverage = mathScoreSum / students.length; System.out.println("该班级各科目的平均成绩:"); System.out.println("语文成绩平均分:" + chineseScoreAverage); System.out.println("英语成绩平均分:" + englishScoreAverage); System.out.println("数学成绩平均分:" + mathScoreAverage); } } ``` 输出结果: ``` 该班级每位同学的平均成绩: 小明:85.0 小红:85.0 小刚:85.0 小李:90.0 小张:85.0 小王:80.0 小陈:85.0 小杨:85.0 小胡:85.0 小林:90.0 该班级各科目的平均成绩: 语文成绩平均分:86.0 英语成绩平均分:85.0 数学成绩平均分:85.0 ``` 这样,我们就完成了这个程序的编写。

相关推荐

最新推荐

recommend-type

钢桁架结构振动特性渐变分析工具

钢桁架结构振动特性渐变分析工具
recommend-type

数据库实战-收集一些常见的 MySQL 死锁案例.zip

数据库实战-收集一些常见的 MySQL 死锁案例.zip 数据库实战-收集一些常见的 MySQL 死锁案例.zip 在工作过程中偶尔会遇到死锁问题,虽然这种问题遇到的概率不大,但每次遇到的时候要想彻底弄懂其原理并找到解决方案却并不容易。这个项目收集了一些常见的 MySQL 死锁案例,大多数案例都来源于网络,并对其进行分类汇总,试图通过死锁日志分析出每种死锁的原因,还原出死锁现场。 实际上,我们在定位死锁问题时,不仅应该对死锁日志进行分析,还应该结合具体的业务代码,或者根据 binlog,理出每个事务执行的 SQL 语句。
recommend-type

Android的移动应用与php服务器交互实例源码.rar

Android的移动应用与php服务器交互实例源码.rar
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这