回复 楼主 大热
public class Student {
private int id;
private String name;
private int english;
private int chinese;
private int math;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
}
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Student[] students = new Student[5];
Scanner sc =new Scanner(System.in);
for(int i = 0 ; i < students.length; i++){
Student s = new Student();
System.out.println("请输入学生的姓名");
String name=sc.nextLine();
System.out.println("请输入学生的学号");
String number =sc.nextLine();
System.out.println("请输入学生的成绩,分别为语文,数学,英语");
int chiness =sc.nextInt();
int math =sc.nextInt();
int english =sc.nextInt();
sc.nextLine();
s.setName(name);
s.setNumber(number);
s.setChiness(chiness);
s.setEnglish(english);
s.setMath(math);
students[i] = s;
}
setSumAll(students);
double[] averageArray = average(students);
for(double d : averageArray){
System.out.println("每个同学平均分的成绩分别为"+d);
}
System.out.println("--------------");
Student stu = max(students);
System.out.println("最高分的学生是"+stu.getName()+" "+"学号是"+stu.getNumber());
}
public static void setSumAll(Student[] students){
for(int i = 0 ; i < students.length; i++){
int chinese = students[i].getChiness();
int english = students[i].getEnglish();
int math = students[i].getMath();
students[i].setSum(chinese+english+math);
}
}
public static double[] average(Student[] students){
double[] arr = new double[5];
for(int i = 0 ; i < students.length; i++){
double average = students[i].getSum()/3.0;
arr[i] = average;
}
return arr;
}
public static Student max(Student[] students){
Student maxStudent = students[0];
for(int i = 0 ; i < students.length; i++)
if(maxStudent.getSum() < students[i].getSum()){
maxStudent = students[i];
System.out.println("最高分的学生成绩是"+maxStudent);
}
return maxStudent;
}
}