HW4.9

时间:2021-11-21 22:34:25

HW4.9

 import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);

         System.out.print("Enter the number of students: ");
         int numberOfStudents = input.nextInt();

         int score = 0;
         int max = 0;
         int secondMax = 0;
         String student = "";
         String maxStudent = "";
         String secondMaxStudent = "";

         for(int i = 0; i < numberOfStudents; i++)
         {
             System.out.print("Enter a student's name: ");
             student = input.next();

             System.out.print("Enter his score: ");
             score = input.nextInt();

             if(score > max)
             {
                 secondMax = max;
                 secondMaxStudent = maxStudent;
                 max = score;
                 maxStudent = student;

             }
         }

         input.close();

         System.out.println("The one has best score is " + maxStudent + ", his score is " + max);
         System.out.println("The one has second score is " + secondMaxStudent + ", his score is " + secondMax);
     }
 }

相关文章