package scheight;
public class Student { int height;
}
package scheight;
import java.util.*;
public class Height {
public Student getHigher(Student[] stu){
Arrays.sort(stu);
return stu[stu.length-1];
}
public static void main(String[] args){
Height h=new Height();
Student[] stu=new Student[10];
Scanner input=new Scanner(System.in);
for(int i=0;i<stu.length;i++){
System.out.print("请输入第"+(i+1)+"个学生的身高:");
stu[i]=new Student();
stu[i].height=input.nextInt();
}
Student s=h.getHigher(stu);
System.out.print(s);
}
}
compare方法还没学,想问下如果不比较数组,通过sort方法进行排序每个对象的身高数值,可以实现吗?
追答貌似不行的 你要用容器的排序方法 而且传的是自定义的对象是不能直接比较的
而且LZ你要知道 System.out.print(s);这句话就算打印出来了 你也得不到你想要的结果 这个将会打印s这个Studnet对象在内存中的地址
谢谢,已经通过循环比较的方法实现!
追答其实LZ你不就是为实现得到最高身高的同学吗 比较的实际上是身高 其实应该把这个比较封装在Studnet中 不过你是这样写的 我就随便改了下 这样功能是能实现了 但是并不是规范的
class Student {
int height;
}
public class Height {
public static void main(String[] args){
int temp = 0;//用于记录身高
int id=0;//用于记录姓名
Student[] stu=new Student[5];
Scanner input=new Scanner(System.in);
for(int i=0;itemp){
temp = stu[i].height;
id = i+1;
}
}
System.out.print("第"+id+"个同学身高最高"+"---------->"+temp);
}
}
请问应该怎么修改。如果必须用sort方法做的话,我是初学者,很多不懂,谢谢了!