[Java] 从键盘中读入三个学生的信息,比较他们的成绩,按照成绩由高到低排列输出 →→→→→进入此内容的聊天室

来自 , 2019-09-13, 写在 Java, 查看 105 次.
URL http://www.code666.cn/view/375c7134
  1. import java.util.Scanner;
  2. public class Main{
  3.         public static void main(String[] args) {
  4.          Scanner scan = new Scanner(System.in);
  5.          
  6.          Student stu[] = new Student [3];
  7.          int i, j;
  8.          for(i = 0;i < 3; i++)
  9.          {
  10.                  int no = scan.nextInt();      
  11.              String name = scan.next();      
  12.              int score = scan.nextInt();
  13.              stu[i] = new Student(no, name, score);
  14.          }
  15.          
  16.          for(i = 0; i < 2; i++)
  17.          {
  18.                  for(j = i+1; j< 3; j++)
  19.                  {
  20.                          Student S = new Student(0, "", 0);
  21.                  if(stu[i].getScore() < stu[j].getScore())
  22.                  {
  23.                          S = stu[i];
  24.                          stu[i] = stu[j];
  25.                          stu[j] = S;
  26.                  }
  27.                  }
  28.          }
  29.          
  30.          for(i = 0; i < 3; i++)
  31.          {
  32.                  stu[i].print();
  33.          }
  34.          scan.close();
  35.     }
  36. }
  37.  
  38. class Student {
  39.         private int no;
  40.         private String name;
  41.     private int score;
  42.        
  43.         public Student(int _no, String _name, int _score) {
  44.                 no = _no;
  45.                 name = _name;
  46.         score = _score;
  47.         }
  48.         public int getNo() {return no;}
  49.         public String getName() {return name;}
  50.         public int getScore() {return score;}
  51.        
  52.         public void print(){
  53.                 System.out.println(no + " "+name+" "+score);
  54.         }
  55. }

回复 "从键盘中读入三个学生的信息,比较他们的成绩,按照成绩由高到低排列输出"

这儿你可以回复上面这条便签

captcha