按以下要求编写一个程序:1请用javaBean规范设计一个学生类Student具有: 属性:no学号、name姓名 和score成绩 功能:输出一位学生的数据内容toString、计算学生的平均分getAvg2在main方法中创建五个学生信息并定义一个对象数组用于存储创建的五位学生的数据 使用Student类的getAvg方法计算出这些学生的平均分;并打印所有学生的信息。思路: 在St
public class Student {
private String no;
private String name;
private int score;
public Student(String no, String name, int score) {
this.no = no;
this.name = name;
this.score = score;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public double getAvg(Student[] students) {
int sum = 0;
for (Student student : students) {
sum += student.getScore();
}
return (double) sum / students.length;
}
@Override
public String toString() {
return "Student{" +
"no='" + no + '\'' +
", name='" + name + '\'' +
", score=" + score +
'}';
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[5];
students[0] = new Student("001", "Tom", 80);
students[1] = new Student("002", "Jerry", 90);
students[2] = new Student("003", "Alice", 85);
students[3] = new Student("004", "Bob", 75);
students[4] = new Student("005", "Lisa", 95);
Student student = new Student("", "", 0);
double avgScore = student.getAvg(students);
System.out.println("平均分:" + avgScore);
for (Student s : students) {
System.out.println(s);
}
}
}
``
原文地址: http://www.cveoy.top/t/topic/h8Pf 著作权归作者所有。请勿转载和采集!