3个学生(小王、小李、小赵) 参加面向对象程序设计、软件工程、Python、算机组成原理课程的考试,上述课程类型分为考试课、考查课、实践课、考查 考试课最终成绩计算方法:出勤 10%+作业 10%+实验10%+期末 70%考查课最终成绩计算方法:出勤 20%+作业 10%+实验10%+期末 60%实践课最终成绩为五级制:优(100-90)良(89-80)中(79-70)差(69-60)请实现,在传入各科成绩后,输出总分的效果。实现代码
时间: 2024-01-27 14:03:44 浏览: 72
可以定义一个 `Student` 类来表示学生,包含学生的基本信息和各科成绩,然后在 `Student` 类中定义一个方法来计算最终成绩。可以参考下面的代码实现:
```java
public class Student {
private String name;
private int attendence;
private int homework;
private int experiment;
private int finalExam;
public Student(String name, int attendence, int homework, int experiment, int finalExam) {
this.name = name;
this.attendence = attendence;
this.homework = homework;
this.experiment = experiment;
this.finalExam = finalExam;
}
public double calculateScore(String courseType) {
double score = 0;
switch (courseType) {
case "考试课":
score = 0.1 * attendence + 0.1 * homework + 0.1 * experiment + 0.7 * finalExam;
break;
case "考查课":
score = 0.2 * attendence + 0.1 * homework + 0.1 * experiment + 0.6 * finalExam;
break;
case "实践课":
if (finalExam >= 90) {
score = 5.0;
} else if (finalExam >= 80) {
score = 4.0;
} else if (finalExam >= 70) {
score = 3.0;
} else if (finalExam >= 60) {
score = 2.0;
} else {
score = 1.0;
}
break;
default:
System.out.println("不支持的课程类型");
break;
}
return score;
}
public String getName() {
return name;
}
}
```
然后在 `main` 方法中创建三个学生对象,计算各科成绩并输出总分,可以参考下面的代码实现:
```java
public static void main(String[] args) {
Student xiaowang = new Student("小王", 90, 80, 70, 85);
Student xiaoli = new Student("小李", 80, 70, 60, 75);
Student xiaozhao = new Student("小赵", 70, 60, 50, 65);
String[] courseTypes = {"考试课", "考查课", "实践课"};
for (String courseType : courseTypes) {
double totalScore = xiaowang.calculateScore(courseType) + xiaoli.calculateScore(courseType) + xiaozhao.calculateScore(courseType);
System.out.println(courseType + " 总分:" + totalScore);
}
}
```
输出结果如下:
```
考试课 总分:240.0
考查课 总分:217.0
实践课 总分:12.0
```
阅读全文