第6章_数组_编程练习_Exercise6_19(成绩排名)

来源:互联网 发布:黎明三角洲淘宝店铺 编辑:程序博客网 时间:2024/04/30 05:32

/**
 * 输入学生成绩和名字,按降序排列
 */
import javax.swing.JOptionPane;

public class Exercise6_19 {
 public static void main(String[] args) {
  String temp;
  String str = "";
  int number =  Integer.parseInt(JOptionPane.showInputDialog("请输入学生数量:"));
  int[] score = new int[number];
  String[] name = new String[number];
  for(int i = 0; i < number; i++){
   score[i] =  Integer.parseInt(JOptionPane.showInputDialog("请输入学生分数:"));
   name[i] = JOptionPane.showInputDialog("输入学生姓名");
  }
  
  //冒泡排序法
  for(int i = 0 ; i < score.length-1 ; i++){
   for(int j = i + 1; j < score.length; j++ ){
    if( score[j] > score[i]){
     score[i] = score[j]+score[i]; 
     score[j] = score[i] - score[j];
     score[i] = score[i] - score[j];
     temp = name[j];
     name[j] = name[i];
     name[i] = temp;
    } 
   }
  }
  //打印成绩表
  for(int i =0; i < score.length; i++){
   str += score[i] + "              " + name[i] + "/n";
  }
  JOptionPane.showMessageDialog(null, "学生成绩:" + "   " + "学生的名字:/n"+str);
 }
}

原创粉丝点击