1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 9 System.out.print("Enter the number of students: "); 10 int numberOfStudents = input.nextInt(); 11 12 int score = 0; 13 int max = 0; 14 int secondMax = 0; 15 String student = ""; 16 String maxStudent = ""; 17 String secondMaxStudent = ""; 18 19 for(int i = 0; i < numberOfStudents; i++) 20 { 21 System.out.print("Enter a student‘s name: "); 22 student = input.next(); 23 24 System.out.print("Enter his score: "); 25 score = input.nextInt(); 26 27 if(score > max) 28 { 29 secondMax = max; 30 secondMaxStudent = maxStudent; 31 max = score; 32 maxStudent = student; 33 34 } 35 } 36 37 input.close(); 38 39 System.out.println("The one has best score is " + maxStudent + ", his score is " + max); 40 System.out.println("The one has second score is " + secondMaxStudent + ", his score is " + secondMax); 41 } 42 }
时间: 2024-11-05 18:54:51