酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

成绩计算-2-关联类 分数 50 作者 蔡轲 单位 南昌航空大学 创建成绩类,包含: 属性:平时成绩(int)、期末成绩(int) 方法:计算总成绩(计算规则:平时成绩*0.4+期末成绩*0.6,保留整数部分,小数部分直接丢弃) 创建学生类,包含: 属性:学号(String)、姓名(String)、语文成绩(成绩类)、数学成绩(成绩类)、物理成绩(成绩类) 方法:计算总分、计算平均分 输入3个学生的信息,将每个学生的信息封装在一个学生对象中。 按输入顺序依次输出3个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。 浮点数保留小数的相关知识可参考:https://blog.csdn.net/huaishuming/article/details/17752365 注意:未用学生类对象封装数据的,本题计0分

```java import java.util.Scanner; class Score { int usualScore; int finalScore; // 计算总成绩的方法 public int calculateTotalScore() { return (int) (usualScore * 0.4 + finalScore * 0.6); } } class Student { String studentId; String name; Score chineseScore; Score mathScore; Score physicsScore; // 计算总分的方法 public int calculateTotalScore() { return chineseScore.calculateTotalScore() + mathScore.calculateTotalScore() + physicsScore.calculateTotalScore(); } // 计算平均分的方法 public double calculateAverageScore() { int totalScore = calculateTotalScore(); return Math.round((double) totalScore / 3 * 100.0) / 100.0; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Student[] students = new Student[3]; for (int i = 0; i < 3; i++) { Student student = new Student(); System.out.print("请输入第 " + (i + 1) + " 个学生的学号: "); student.studentId = scanner.nextLine(); System.out.print("请输入第 " + (i + 1) + " 个学生的姓名: "); student.name = scanner.nextLine(); System.out.println("请输入语文成绩的平时成绩和期末成绩: "); Score chineseScore = new Score(); chineseScore.usualScore = scanner.nextInt(); chineseScore.finalScore = scanner.nextInt(); student.chineseScore = chineseScore; System.out.println("请输入数学成绩的平时成绩和期末成绩: "); Score mathScore = new Score(); mathScore.usualScore = scanner.nextInt(); mathScore.finalScore = scanner.nextInt(); student.mathScore = mathScore; System.out.println("请输入物理成绩的平时成绩和期末成绩: "); Score physicsScore = new Score(); physicsScore.usualScore = scanner.nextInt(); physicsScore.finalScore = scanner.nextInt(); student.physicsScore = physicsScore; students[i] = student; } for (Student student : students) { System.out.println("总分: " + student.calculateTotalScore()); System.out.println("平均分: " + student.calculateAverageScore()); } } } ``` 额外需要注意的逻辑点: 1. 输入数据时要确保数据的合法性,例如成绩不能为负数。 2. 在处理浮点数计算平均分时,要注意精度问题,避免出现误差。 3. 代码中的输入部分可以添加异常处理,以应对用户输入非法数据的情况。 [2025-04-18 11:41:55 | AI写代码神器 | 655点数解答]

相关提问