java中当需要求所有学生成绩的最大值时 ,而此时最大值为多个学生,要求能够同时输出所有最大值学生的姓名
5个回答
展开全部
这个你可以用一个map吧。
把学生名字作为key,成绩作为value。
先判断出最好成绩是多少。然后根据这个成绩去匹对value,输出key。
后来想了一下,用list可能比较好循环:
代码如下:
public void getMaxScore() {
List<String[]> student = new ArrayList<String[]>();
student.add(new String[] { "aronld", "98" });
student.add(new String[] { "blue", "95" });
student.add(new String[] { "green", "96" });
student.add(new String[] { "yellow", "98" });
student.add(new String[] { "grey", "98" });
student.add(new String[] { "red", "98" });
student.add(new String[] { "perpuse", "94" });
student.add(new String[] { "white", "94" });
student.add(new String[] { "noviziki", "96" });
student.add(new String[] { "messi", "95" });
student.add(new String[] { "ronald", "98" });
int[] score = new int[student.size()];
;
for (int i = 0; i < score.length; i++) {
score[i] = Integer.valueOf(student.get(i)[1]);
}
int max = 0;
for (int i = 0; i < student.size(); i++) {
if (score[i] > max) {
max = score[i];
}
}
for (String[] stu : student) {
if (Integer.valueOf(stu[1]) == max) {
System.out.println("student name : " +stu[0]+"----score = "+stu[1] );
}
}
}
这样就可以输出成绩最高的学生姓名和成绩了.
结果:
student name : aronld----score = 98
student name : yellow----score = 98
student name : grey----score = 98
student name : red----score = 98
student name : ronald----score = 98
把学生名字作为key,成绩作为value。
先判断出最好成绩是多少。然后根据这个成绩去匹对value,输出key。
后来想了一下,用list可能比较好循环:
代码如下:
public void getMaxScore() {
List<String[]> student = new ArrayList<String[]>();
student.add(new String[] { "aronld", "98" });
student.add(new String[] { "blue", "95" });
student.add(new String[] { "green", "96" });
student.add(new String[] { "yellow", "98" });
student.add(new String[] { "grey", "98" });
student.add(new String[] { "red", "98" });
student.add(new String[] { "perpuse", "94" });
student.add(new String[] { "white", "94" });
student.add(new String[] { "noviziki", "96" });
student.add(new String[] { "messi", "95" });
student.add(new String[] { "ronald", "98" });
int[] score = new int[student.size()];
;
for (int i = 0; i < score.length; i++) {
score[i] = Integer.valueOf(student.get(i)[1]);
}
int max = 0;
for (int i = 0; i < student.size(); i++) {
if (score[i] > max) {
max = score[i];
}
}
for (String[] stu : student) {
if (Integer.valueOf(stu[1]) == max) {
System.out.println("student name : " +stu[0]+"----score = "+stu[1] );
}
}
}
这样就可以输出成绩最高的学生姓名和成绩了.
结果:
student name : aronld----score = 98
student name : yellow----score = 98
student name : grey----score = 98
student name : red----score = 98
student name : ronald----score = 98
展开全部
一个sql就能搞定的啊
假设姓名name在表tb_student里面,成绩score在tb_study中,两张表有共同的字段stuid(学生id)
select tb_student.name from tb_student,tb_study where tb_student.stuid=tb_study.stuid and tb_study.score=(select max(score) as score from tb_study)
或者用外连接
select A.name as name
from (tb_student as A left join tb_study as B on A.stuid=B.stuid)tba
where B.score=(select max(score) as score from tb_study)
假设姓名name在表tb_student里面,成绩score在tb_study中,两张表有共同的字段stuid(学生id)
select tb_student.name from tb_student,tb_study where tb_student.stuid=tb_study.stuid and tb_study.score=(select max(score) as score from tb_study)
或者用外连接
select A.name as name
from (tb_student as A left join tb_study as B on A.stuid=B.stuid)tba
where B.score=(select max(score) as score from tb_study)
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
求出最大值,然后把成绩是最大值的学生姓名输出可以吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这得贴一堆的代码。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你太懒了吧。。。这都问..
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询