java编程实现:输入班里10名学生的身高,获得身高最高的学生。要求使用对象数组类型带参方法来实现。

怎么定位最高身高的学生是第几名?有没有详细代码?
1.定义Students类,添加身高等属性
2.定义Height类,定义方法getMaxHeight()
public Students getMaxHeight(Students[] stu) {
}
求详解。。。。

我的理解你想用一个方法直接实现的话建议方法可以这样设置

方法返回最高身高的那个学生在数组中的下标位置
然后直接从数组中获得同学对象,就可以实现你要求的功能了

public static void main() {
.....
int sub = getMaxHeight(stu);
System.out.println("第" + (sub+1) + "名学生身高最高,为" + stu[sub].getHeight());

}

public int getMaxHeight(Students[] stu) {
int sub = 0; // 最高学生在数组中的下标
int maxHeight = 0; // 当前最高身高
for (int i = 0; i < stu.length; i++) {
Students s = stu[i];
if(s.getHeight() > maxHeight) {
sub = i;
maxHeight = s.getHeight();
}

}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-07-13
public class Student implements Comparable<Student>
{
int index;
double heigth;
public Student(int index, double heigth)
{
this.index = index;
this.heigth = heigth;
}
public int getIndex()
{
return index;
}
public void setIndex(int index)
{
this.index = index;
}
public double getHeigth()
{
return heigth;
}
public void setHeigth(double heigth)
{
this.heigth = heigth;
}
@Override
public int compareTo(Student o)
{
if (heigth > o.heigth)
{
return -1;
}
else if (heigth == o.heigth)
{
return 0;
}
else
{
return 1;
}
}
}

public class Height
{
public Student getMaxHeigth(Student[] students)
{
Arrays.sort(students);
return students[0];
}
public static void main(String[] args)
{
int i = 0;
Student[] students = new Student[10];
while (i < 10)
{
System.out.print("请输入第" + (i + 1) + "个学生的升高:");
Scanner scanner = new Scanner(System.in);
double heigth = scanner.nextDouble();
students[i] = new Student(i + 1, heigth);
i++;
}
Height height = new Height();
Student student = height.getMaxHeigth(students);
System.out.println("该班第" + student.getIndex() + "学生身高最高为:"
+ student.getHeigth());
}
}追问

public Student(int index, double heigth)
{
this.index = index;
this.heigth = heigth;
这段代码中的this是什么意思呢??

请大虾解释下!我是初学java

第2个回答  2012-07-12
数组的排序功能,应该每本书上都有例子吧追问

如图片显示 : 身高最高的是180 是第二个学生。 要用什么方法输出:第二名学生

相似回答