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

最好写完整了,要有输出的功能,小弟初学者,最好简单能看懂的

需要10个学生就在创建5个学生,然后map进去。我懒得写了。

package package2;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<Integer, Student> map = new HashMap<Integer, Student>();
Student stu1 = new Student();
stu1.setName("第一个学生");
stu1.setHeigth(180);
Student stu2 = new Student();
stu2.setName("第二个学生");
stu2.setHeigth(170);
Student stu3 = new Student();
stu3.setName("第三个学生");
stu3.setHeigth(185);
Student stu4 = new Student();
stu4.setName("第四个学生");
stu4.setHeigth(187);
Student stu5 = new Student();
stu5.setName("第五个学生");
stu5.setHeigth(175);
map.put(stu1.getHeigth(), stu1);
map.put(stu2.getHeigth(), stu2);
map.put(stu3.getHeigth(), stu3);
map.put(stu4.getHeigth(), stu4);
map.put(stu5.getHeigth(), stu5);
int[] array = { stu1.getHeigth(), stu2.getHeigth(), stu3.getHeigth(),
stu4.getHeigth(), stu5.getHeigth() };
int a;
for (int i = 0; i < array.length; i++) {
for (int j = 0 + i; j < array.length; j++) {
if (array[i] < array[j]) {
a = array[i];
array[i] = array[j];
array[j] = a;
}
}
}
int c = array[0];
Student s = map.get(c);
System.out.println(s.getName() + "身高最高为:" + c + "厘米。");
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-17
相似回答