首页 > 试题广场 >

(找出两个分教最高的学生)编写程序,提示用户输人学生的个数、

[问答题]
(找出两个分教最高的学生)编写程序,提示用户输人学生的个数、每个学生的名宇及其分数,最 后显示获得最高分的学生和第二高分的学生。
public class Test {
	public static void main(String[] args){
	
		//程序说明:找出分数最高的学生
		//提示用户输入
		System.out.print("Enter the number of students:");
		
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		String str = "",strh = "",strs = "";
		double score = 0,high = 0,second = 0;
			//输入学生信息
		int i = 1;
		while(i <= n)
		{
			System.out.print("Enter the name of the student:");
			str = input.next();
			System.out.print("Enter the score of the student:");
			score = input.nextDouble();
			
			if(score > high)
				{
				second = high;
				strs = strh;
				high = score;
				strh = str;
				}
			else if(score > second)
			{
				second = score;
				strs = str;
			}
			i++;
		}
		//显示分数最高的学生
		System.out.println("name of the top student:" + strh + "."+  "score of the top student:" + high);
		System.out.println("name of the second student:" + strs + "."+  "score of the second student:" + second);
    }
}

发表于 2020-02-28 14:54:13 回复(0)