用java设计一People类,包含name(String)、age(int)属性与相关设定、读取方法。 设计Student类,继承People类,属性有修习的课程分数,属性名称为chinese、english、math三科目成绩,并写出设定与读取分数的方法,以及计算三科平均分数的方法。 设计一程序,可以持续输入学生(最多十位)的分数,并可以计算个人的平均分数,各科的平均分数,以及全体的总平均。
时间: 2024-02-13 09:00:43 浏览: 77
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
好的,以下是代码实现:
People类:
```java
public class People {
private String name;
private int age;
public People(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
Student类:
```java
public class Student extends People {
private double chinese;
private double english;
private double math;
public Student(String name, int age, double chinese, double english, double math) {
super(name, age);
this.chinese = chinese;
this.english = english;
this.math = math;
}
public double getChinese() {
return chinese;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getAverage() {
return (chinese + english + math) / 3;
}
}
```
主程序:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[10];
int count = 0;
double chineseSum = 0;
double englishSum = 0;
double mathSum = 0;
while (true) {
System.out.print("请输入学生的姓名:");
String name = scanner.next();
System.out.print("请输入学生的年龄:");
int age = scanner.nextInt();
System.out.print("请输入学生的语文成绩:");
double chinese = scanner.nextDouble();
System.out.print("请输入学生的英语成绩:");
double english = scanner.nextDouble();
System.out.print("请输入学生的数学成绩:");
double math = scanner.nextDouble();
Student student = new Student(name, age, chinese, english, math);
students[count++] = student;
chineseSum += chinese;
englishSum += english;
mathSum += math;
System.out.print("是否继续输入(Y/N):");
String choice = scanner.next();
if (choice.equals("N") || count == 10) {
break;
}
}
double chineseAverage = chineseSum / count;
double englishAverage = englishSum / count;
double mathAverage = mathSum / count;
double totalAverage = (chineseSum + englishSum + mathSum) / (count * 3);
System.out.println("各科平均分数:");
System.out.println("语文:" + chineseAverage);
System.out.println("英语:" + englishAverage);
System.out.println("数学:" + mathAverage);
System.out.println("全体总平均分数:" + totalAverage);
for (int i = 0; i < count; i++) {
System.out.println("学生" + (i + 1) + ":" + students[i].getName());
System.out.println("个人平均分数:" + students[i].getAverage());
}
}
}
```
使用Scanner类实现控制台输入,定义一个Student类型的数组用于存放学生对象,用count变量记录已输入学生的数量,用chineseSum、englishSum、mathSum变量分别记录各科的总分数,最后计算各项平均分数并输出。
阅读全文