建立测试类(Test)
import java.util.Scanner;
//建立测试类
public class Test {
//主方法(main)
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
//定义学生数组
Student[] stu=new Student[3];
//创建两个对象
Student A=new Student();
Height h=new Height();
//遍历数组
for(int i=0;i<3;i++){
System.out.println("请输入第"+(i+1)+"个学生的java成绩:");
System.out.println("请输入第"+(i+1)+"个学生的姓名:");
stu[i]=new Student();
stu[i].id=i+1;
stu[i].score=input.nextInt();
stu[i].name=input.next();
}
//调用方法getMaxScore(stu)
A=h.getMaxScore(stu);
//输出结果
System.out.println("分最高的学生的姓名是:"+A.name");
System.out.println("java成绩是:"+A.score);
}
}
定义学生类
package HomeWork4;
public class Student {
//定义3个变量
int score;
String name;
int id;
}
定义Height(功能类)类
package HomeWork4;
//定义height类
public class Height {
//定义方法
public Student getMaxScore(Student[] s){
//创建对象
Student A=new Student();
for(int i=0;i<s.length;i++){
if(s[i].score>A.score){
A.score=s[i].score;
A.id=s[i].id;
A.name=s[i].name;
}
}
//返回数据
return A;
}
}
代码运行结果如下: