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

创建成绩类,包含: 属性:平时成绩(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; int calculateTotalScore() { return (int) (usualScore * 0.4 + finalScore * 0.6); } } class Student { String studentId; String name; Score chineseScore; Score mathScore; Score physicsScore; int calculateTotalScore() { return chineseScore.calculateTotalScore() + mathScore.calculateTotalScore() + physicsScore.calculateTotalScore(); } double calculateAverageScore() { return (double) calculateTotalScore() / 3; } } 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("请输入第" + (i + 1) + "个学生的语文平时成绩和期末成绩:"); Score chineseScore = new Score(); chineseScore.usualScore = scanner.nextInt(); chineseScore.finalScore = scanner.nextInt(); student.chineseScore = chineseScore; System.out.println("请输入第" + (i + 1) + "个学生的数学平时成绩和期末成绩:"); Score mathScore = new Score(); mathScore.usualScore = scanner.nextInt(); mathScore.finalScore = scanner.nextInt(); student.mathScore = mathScore; System.out.println("请输入第" + (i + 1) + "个学生的物理平时成绩和期末成绩:"); Score physicsScore = new Score(); physicsScore.usualScore = scanner.nextInt(); physicsScore.finalScore = scanner.nextInt(); student.physicsScore = physicsScore; students[i] = student; scanner.nextLine(); // 处理输入流中的换行符 } for (Student student : students) { System.out.println("总分:" + student.calculateTotalScore()); System.out.printf("平均分:%.2f\n", student.calculateAverageScore()); } } } ``` [2024-11-19 15:54:02 | AI写代码神器 | 581点数解答]

相关提问