定义一个测试类StudentTest,创建一个Student对象,对其进行赋值并计算总分与平均分
时间: 2024-05-12 16:15:19 浏览: 16
以下是一个可能的实现:
```java
public class StudentTest {
public static void main(String[] args) {
Student student = new Student(); // 创建一个Student对象
// 给学生对象赋值
student.setName("张三");
student.setChineseScore(80);
student.setMathScore(90);
student.setEnglishScore(85);
// 计算总分和平均分
int totalScore = student.getChineseScore() + student.getMathScore() + student.getEnglishScore();
double averageScore = totalScore / 3.0;
// 输出结果
System.out.println("姓名:" + student.getName());
System.out.println("语文成绩:" + student.getChineseScore());
System.out.println("数学成绩:" + student.getMathScore());
System.out.println("英语成绩:" + student.getEnglishScore());
System.out.println("总分:" + totalScore);
System.out.println("平均分:" + averageScore);
}
}
```
需要先定义一个 `Student` 类,该类包含姓名和语文、数学、英语三门科目的成绩,并提供相应的访问器和修改器方法,以及计算总分和平均分的方法。例如:
```java
public class Student {
private String name;
private int chineseScore;
private int mathScore;
private int englishScore;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
public int getTotalScore() {
return chineseScore + mathScore + englishScore;
}
public double getAverageScore() {
return getTotalScore() / 3.0;
}
}
```
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)