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

创建成绩类,包含: 属性:平时成绩(int)、期末成绩(int) 方法:计算总成绩(计算规则:平时成绩*0.4+期末成绩*0.6,保留整数部分,小数部分直接丢弃) 创建学生类,包含: 属性:学号(string)、姓名(string)、语文成绩(成绩类)、数学成绩(成绩类)、物理成绩(成绩类) 方法:计算总分、计算平均分 输入3个学生的信息,将每个学生的信息封装在一个学生对象中。 按输入顺序依次输出3个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。

以下是使用 Java 实现的代码: ```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() { 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.print("请输入第" + (i + 1) + "个学生的语文平时成绩: "); int usualChineseScore = scanner.nextInt(); System.out.print("请输入第" + (i + 1) + "个学生的语文期末成绩: "); int finalChineseScore = scanner.nextInt(); student.chineseScore = new Score(); student.chineseScore.usualScore = usualChineseScore; student.chineseScore.finalScore = finalChineseScore; System.out.print("请输入第" + (i + 1) + "个学生的数学平时成绩: "); int usualMathScore = scanner.nextInt(); System.out.print("请输入第" + (i + 1) + "个学生的数学期末成绩: "); int finalMathScore = scanner.nextInt(); student.mathScore = new Score(); student.mathScore.usualScore = usualMathScore; student.mathScore.finalScore = finalMathScore; System.out.print("请输入第" + (i + 1) + "个学生的物理平时成绩: "); int usualPhysicsScore = scanner.nextInt(); System.out.print("请输入第" + (i + 1) + "个学生的物理期末成绩: "); int finalPhysicsScore = scanner.nextInt(); student.physicsScore = new Score(); student.physicsScore.usualScore = usualPhysicsScore; student.physicsScore.finalScore = finalPhysicsScore; scanner.nextLine(); // 处理输入流中的换行符 students[i] = student; } for (int i = 0; i < 3; i++) { System.out.println("第" + (i + 1) + "个学生的总分: " + students[i].calculateTotalScore()); System.out.printf("第" + (i + 1) + "个学生的平均分: %.2f\n", students[i].calculateAverageScore()); } } } ``` 这段代码首先创建了`Score`类来表示成绩,包含平时成绩和期末成绩,以及计算总成绩的方法。然后创建`Student`类来表示学生,包含学号、姓名和各个科目的成绩对象,以及计算总分和平均分的方法。在`main`方法中,通过用户输入创建学生对象并存储在数组中,最后计算并输出每个学生的总分和平均分。 [2024-10-11 08:45:50 | AI写代码神器 | 847点数解答]

相关提问