编写程序,提示输入学生的个数、每个学生的名字及其分数,最后显示获得最高分的学生和第二高分的学生。
import java.util.Scanner;
public class Grade {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入学生的个数");
int numofstudents=scanner.nextInt();
String highestScorerName = "";
int highestscore=Integer.MIN_VALUE;
String secondhighestScorerName = "";
int secondhighestscore=Integer.MIN_VALUE;
for (int i=1;i<=numofstudents;i++){
System.out.println("请输入第"+i+"个学生的名字");
String studentName=scanner.next();
System.out.println("请输入第"+i+"个学生的分数");
int studentscore=scanner.nextInt();
if (studentscore>highestscore){
secondhighestscore=highestscore;
secondhighestScorerName=highestScorerName;
highestscore=studentscore;
highestScorerName=studentName;
} else if (studentscore>secondhighestscore){
secondhighestscore=studentscore;
secondhighestScorerName=studentName;
}
}
System.out.println("获得最高分的学生是"+highestScorerName+"分数为"+highestscore);
System.out.println("获得第二高分的学生是"+secondhighestScorerName+"分数是"+secondhighestscore);
}
}