找出两个最高分

mp900511 2015-04-01 02:39:43
Java编写程序,提示用户输入学生的个数、每个学生的名字及其分数,最后显示获得最高分的学生的名字和第二高分学生的名字。
...全文
383 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_30428739 2016-02-03
  • 打赏
  • 举报
回复
Scanner input = new Scanner(System.in); int count = 0; System.out.println("Enter number of students"); int number = input.nextInt(); System.out.println("Enter a student name"); String student1 = input.next(); System.out.println("Enter a student score"); double score1 = input.nextDouble(); System.out.println("Enter a student name"); String student2 = input.next(); System.out.println("Enter a student score"); double score2 = input.nextDouble(); /* * 保证socre1在socre1和socre2中一定为最大的值 * */ if (score1<score2) { double temp=0; String name=null; temp=score1; score1=score2; score2=temp; name=student1; student1=student2; student2=name; //System.out.println(score1);看交换是否完成, } for (int i = 0; i < number - 2; i++) { System.out.println("Enter a student name"); String student = input.next(); System.out.println("Enter a student score"); double score = input.nextDouble(); /* * 从第三个输入开始于前面两个数比较, * 大于前面两个数中的最大值,score1,则将score1赋给score2,再将score赋给score1, * 若score1>score>score2,则将score赋给score2 * */ if (score>score1) { score2=score1; student2=student1; score1=score; student1=student; } else if (score>score2) { score2=score; student2=student; } else { } } System.out.println("The top student " + student1 + "'s score is " + score1); System.out.println("The second student " + student2 + "'s score is " + score2);
空空掌门 2015-04-04
  • 打赏
  • 举报
回复
什么都不需要,直接定义两个变量记录最高分的最低分的,每次输入都作比较,就行了
code小生 2015-04-03
  • 打赏
  • 举报
回复
这个不是太难,你应该是新手吧?楼上说的都没错,但是对于初学者来说,有些难度,我想还是利用流程控制和循环结构来实现比较和你的心意吧?
三仙半 2015-04-01
  • 打赏
  • 举报
回复
不需要排序,就一趟循环就可以解决,试试我这个代码。
import java.util.ArrayList;
public class Find1_2 {
	public static void main(String[] args) {
		ArrayList<StudentInfo> list = new ArrayList<StudentInfo>();
		//生成实验数据,并显示
		for(int i =0; i<12; i++ ){
			StudentInfo info = new StudentInfo("stu"+(i+1), Math.random()*100);
			System.out.println(info);
			list.add(info);
		}
		StudentInfo info1 = new StudentInfo("tmp", 0);
		StudentInfo info2 = new StudentInfo("tmp", 0);
		int count = list.size();
		for(int i =0; i<count; i++ ){
			StudentInfo info = list.get(i);
			if(info.score>info1.score){
				info2 = info1;
				info1 = info;
			}else if(info.score>info2.score){
				info2 = info;
			}
		}
		//显示运行结果
		System.out.println("第一名是"+info1+", 第二名是"+info2);
	}
}

class StudentInfo{
	double score;
	String name;	
	public StudentInfo(String name, double score){
		this.name = name;
		this.score = score;
	}
	@Override
	public String toString() {
		return "Student[name="+name+",score="+score+"]";
	}
}
xinghuiwu 2015-04-01
  • 打赏
  • 举报
回复
List<StudentInfo> list = new arrayList(); list.put(studentInfo); for (int i = 0; i < list.size()-1; i++){ for(int j = 0 ;j <list.size() - i - 1; j++){ if(list[j].score < list[j + 1].score){ StudentInfo temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } //取出最后两个就是成绩前两名了,不知道有没有错,坐等大神解释
lyw985 2015-04-01
  • 打赏
  • 举报
回复
使用两个变量保存即可
QuickKeyBoard 2015-04-01
  • 打赏
  • 举报
回复
冒泡排序走两趟即可……

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧