找出最高分和第二高分

找出最高分学生和姓名

import java.util.*;
class Main {
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    double maxScoreOfStudent = 0;
    String nameOfMaxScoreStudent = "";
    
    System.out.print("Enter the numbers of the students: ");
    int numbersOfStudents = input.nextInt();
    
    if(numbersOfStudents != 0){
      for(int i=0; i<numbersOfStudents; i++){
        System.out.println("Enter the name and the score of the No." + (i + 1) +
                           " student: ");
        String nameOfstudent = input.next();
        double scoreOfstudent = input.nextDouble();
        
        if(scoreOfstudent > maxScoreOfStudent){
          maxScoreOfStudent = scoreOfstudent;
          nameOfMaxScoreStudent = nameOfstudent;
       }
      }
    }else{
      System.exit(0);
    }
    System.out.print("The highest score student is " + nameOfMaxScoreStudent + 
                    " and score is " + maxScoreOfStudent);
  }
}

找出最高分和第二高分学生和姓名

import java.util.*;
class Main {
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    double maxScoreOfStudent = 0;
    double SecondMaxScoreOfStudent = 0;
    String nameOfMaxScoreStudent = "";
    String nameOfSecondMaxScoreStudent = "";
    
    System.out.print("Enter the numbers of the students: ");
    int numbersOfStudents = input.nextInt();
    
    if(numbersOfStudents != 0){
      for(int i=0; i<numbersOfStudents; i++){
        System.out.println("Enter the name and the score of the No." + (i + 1) +
                           " student: ");
        String nameOfstudent = input.next();
        double scoreOfstudent = input.nextDouble();
        
        if(scoreOfstudent > maxScoreOfStudent){
          SecondMaxScoreOfStudent = maxScoreOfStudent;
          nameOfSecondMaxScoreStudent = nameOfMaxScoreStudent;
          maxScoreOfStudent = scoreOfstudent;
          nameOfMaxScoreStudent = nameOfstudent;
        }else if(scoreOfstudent > SecondMaxScoreOfStudent){
          SecondMaxScoreOfStudent = scoreOfstudent;
          nameOfSecondMaxScoreStudent = nameOfstudent;
        }
      }
      
    }else{
      System.exit(0);
    }
    System.out.print("The highest score student is " + nameOfMaxScoreStudent + 
                    " and score is " + maxScoreOfStudent);
    System.out.print("\nThe second score student is " + nameOfSecondMaxScoreStudent + 
                    " and score is " + SecondMaxScoreOfStudent);
    
    
  }
}
posted @ 2022-05-24 16:16  Scenery_Shelley  阅读(66)  评论(0编辑  收藏  举报