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

来自 , 2020-07-02, 写在 Java, 查看 161 次.
URL http://www.code666.cn/view/b9f94c77
  1. import java.util.Scanner;
  2. public class Main{
  3.     public static void main(String[] args) {
  4.          Scanner scan = new Scanner(System.in);      
  5.          int no = scan.nextInt();      
  6.          String name = scan.next();      
  7.          int score = scan.nextInt();
  8.          Student p1 = new Student(no,name,score);
  9.          int no2 = scan.nextInt();      
  10.          String name2 = scan.next();      
  11.          int score2 = scan.nextInt();
  12.          Student p2 = new Student(no2,name2,score2);
  13.          int no3 = scan.nextInt();      
  14.          String name3 = scan.next();      
  15.          int score3 = scan.nextInt();
  16.          Student p3 = new Student(no3,name3,score3);
  17.          if(p1.getScore() >= p2.getScore())
  18.          {
  19.                  if(p1.getScore() >= p3.getScore())
  20.                  {
  21.                          p1.print();
  22.                          if(p2.getScore() >= p3.getScore())
  23.                          {
  24.                                  p2.print();
  25.                          p3.print();
  26.                          }
  27.                          else if (p2.getScore() < p3.getScore())
  28.                          {
  29.                                  p3.print();
  30.                          p2.print();
  31.                          }
  32.                  }
  33.                  else if (p1.getScore() < p3.getScore())
  34.                  {
  35.                          p3.print();
  36.                          p1.print();
  37.                  p2.print();
  38.                  }
  39.          }
  40.          else if (p1.getScore() < p2.getScore())
  41.          {
  42.                  if(p2.getScore() >= p3.getScore())
  43.                  {
  44.                          p2.print();
  45.                          if(p1.getScore() >= p3.getScore())
  46.                          {
  47.                                  p1.print();
  48.                          p3.print();
  49.                          }
  50.                          else if (p1.getScore() < p3.getScore())
  51.                          {
  52.                                  p3.print();
  53.                          p1.print();
  54.                          }
  55.                  }
  56.                  else if (p2.getScore() < p3.getScore())
  57.                  {
  58.                          p3.print();
  59.                          p2.print();
  60.                  p1.print();
  61.                  }
  62.          }
  63.          scan.close();
  64.     }
  65. }
  66. class Student {
  67.         private int no;
  68.         private String name;
  69.     private int score;
  70.        
  71.         public Student(int _no, String _name, int _score) {
  72.                 no = _no;
  73.                 name = _name;
  74.         score = _score;
  75.         }
  76.         public int getNo() {return no;}
  77.         public String getName() {return name;}
  78.         public int getScore() {return score;}
  79.        
  80.         public void print(){
  81.                 System.out.println(no + " "+name+" "+score);
  82.         }
  83. }

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

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

captcha