import java.io.*;
class chenjiguanlixitong
{
static String [][]  chengji=new String [4][6];
static int DC_scroe,JAVA_scroe,English_score;
static int Total_scroe;
static String name="";
public static void anykey_f()
{
char tChar;
System.out.println("   按任意键继续  ...   ");
try{
tChar=(char)System.in.read();

}catch(IOException e){}
}
    public static void input(){
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        for(int i=0;i<=3;i++)
        {System.out.print("-------------------------------\n");
     System.out.println("请输入学生的学号 :");
     System.out.flush();
     try{chengji[i][0]=in.readLine();
     }catch(IOException e){}
     System.out.println("请输入学生的姓名 :");
     System.out.flush();
     try{chengji[i][1]=in.readLine();
     }catch(IOException e){}
     System.out.println("请输入学生的DC成绩 :");
     System.out.flush();
     try{chengji[i][2]=in.readLine();
     }catch(IOException e){}
     System.out.println("请输入学生的JAVA成绩 :");
     System.out.flush();
     try{chengji[i][3]=in.readLine();
     }catch(IOException e){}
     System.out.println("请输入学生的English成绩 :");
     System.out.flush();
     try{chengji[i][4]=in.readLine();
     }catch(IOException e){}
        } 
         anykey_f();
    }
    public static void count(){
     for(int i=0;i<=3;i++)                       //将数据转化成int型,将进行加法计算
     {
     DC_scroe=Integer.valueOf(chengji[i][2]).intValue();
     JAVA_scroe=Integer.valueOf(chengji[i][3]).intValue();
     English_score=Integer.valueOf(chengji[i][4]).intValue();
     Total_scroe=DC_scroe+JAVA_scroe+English_score;
    
     chengji[i][5]=String.valueOf(Total_scroe);
     }
     for(int i=0;i<=3;i++)
     {
     System.out.print("-------------------------\n");
     System.out.println("学生 :"+chengji[i][1]+" 成绩总分为 "+chengji[i][5]);
     }
     anykey_f();
    }
    public static void bubble_sort(){//冒泡排序
    int temp,flag;
    int size=4;
    int [] total_scroe=new int[4];
    for(int i=0;i<=size-1;i++){     
    total_scroe[i]=Integer.valueOf(chengji[i][5]).intValue();
    }
    for(int i=0;i<3;i++){                          //将数据两两比较,将大的置于前
     flag=0;
     for(int j=0;j<size-i-1;j++)
     if(total_scroe[j]<total_scroe[j+1]){
     flag=1;
     temp=total_scroe[j+1];
     name=chengji[j+1][1];
     total_scroe[j+1]=total_scroe[j];
     chengji[j+1][1]=chengji[j][1];
     total_scroe[j]=temp;
     chengji[j][1]=name;
     }
     if(flag!=1)
     break;
    
    }
    System.out.println("按总分排序结果 :");
    for(int i=0;i<=3;i++){
    System.out.println(chengji[i][1]+"成绩总分:"+total_scroe[i]);
    }
    anykey_f();
    }
    public static void selection_sort()//选择排序
    {
    int temp=0;
    int [] total_scroe=new int[4];
    for(int i=0;i<=3;i++){     
    total_scroe[i]=Integer.valueOf(chengji[i][5]).intValue();
    }
    int base=0,compare=0,max=0,size=4;
    for(base=0;base<size-1;base++){                            //将目前数据与后面数据中最大的对调
     max=base;
     for(compare=base+1;compare<size;compare++)
     if(total_scroe[compare]>total_scroe[max])
     max=compare;
     temp=total_scroe[max];
     name=chengji[max][1];
     total_scroe[max]=total_scroe[base];
     chengji[max][1]=chengji[base][1];
     total_scroe[base]=temp;
     chengji[base][1]=name;
    
    }
    System.out.println("按总分排序结果 :");
    for(int i=0;i<=3;i++){
    System.out.println(chengji[i][1]+"成绩总分:"+total_scroe[i]);
    }
    anykey_f();
    }    public static void query(){
     BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
     String st="";
     int k;
     System.out.print("------------------------------\n");
     System.out.print(" 请输入要查询学生的学号 ");
     System.out.flush();
     try{st=in.readLine();}catch(IOException e){
     }
     for(int i=0;i<=3;i++)
     {   int flag=0;
     if(chengji[i][0].equals(st))
     {   flag=1;
     k=i;
     System.out.println("学号 :"+chengji[k][0]);
     System.out.println("姓名 :"+chengji[k][1]);
     System.out.println("DC成绩 :"+chengji[k][2]);
     System.out.println("JAVA成绩 :"+chengji[k][3]);
     System.out.println("English成绩 :"+chengji[k][4]);
     System.out.println("总分 :"+chengji[k][5]);
     }
     if(flag!=0)
     break;
     if(i==3)
     System.out.println("无此信息记录,请重新输入...");   
     }
     anykey_f();
     }
    public static void main (String[] args) {              //主函数
     BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
     String op="";
     int option=0;
     chenjiguanlixitong obj=new chenjiguanlixitong();
     do{
     System.out.println("*********学生成绩管理系统**********");
            System.out.println("         <1>输入        ");
            System.out.println("         <2>总分统计        ");
            System.out.println("         <3>信息查询        ");
            System.out.println("         <4>总分排序(冒泡排序)        ");
            System.out.println("         <5>总分排序(选择排序)        ");
            System.out.println("         <6>退出        ");
            System.out.println("***********************************");
            op="";
            try{
             op=in.readLine();
            }catch(IOException e){}
            option=0;
            try{
             option=Integer.valueOf(op).intValue();
            }catch(NumberFormatException e){}
            System.out.println("");
            switch(option){
             case 1:
             obj.input();//输入函数
             break;
             case 2:
             obj.count();//统计总分函数
             break;
             case 3:
             obj.query();//查询函数
             break;
             case 4:
             obj.bubble_sort(); //冒泡排序函数
             break;
             case 5:
             obj.selection_sort(); //选择排序函数
             break;
             case 6:
             System.exit(0);  //退出程序
            
            }
     }while(true);
}
}
这是我写的一个简单的成绩管理程序..
用二维数组实现的...在排序上出现点小麻烦...
排序是根据 总分 来排序, 是降序.这就出现一个问题,单单只有总分数值进行了排序,而其他相应信息没有动.这就造成了信息对不上号...上面的代码中,我只简单地把姓名和相应的总分进行了对应的排序...
出现了一个奇怪现象:输入完毕后,先进行一次冒泡后,总分与相应的名字是匹配的,但再进行冒泡或者选择排序,总分与姓名就不匹配了...虽然总分还是降序的...针对以上问题...我想请教下,  该使用什么方法来解决 排序时,相应信息都是匹配的,而不是仅仅总分数字进行了单一排序...另外,这个程序如果输入中文,会显示显示乱码...如何解决....分数不多...只能50..拜托啦...

解决方案 »

  1.   

    用我下面的,替换掉你原来的bubble_sort()public static void bubble_sort(){//冒泡排序 
        int temp,flag; 
        int size=4; 
        int [] total_scroe=new int[4]; 
        String[] temp;
        for(int i=0;i <=size-1;i++){ 
         total_scroe[i]=Integer.valueOf(chengji[i][5]).intValue(); 
        } 
        for(int i=0;i <3;i++){                          //将数据两两比较,将大的置于前 
            flag=0; 
            for(int j=0;j <size-i-1;j++) 
               if(total_scroe[j] <total_scroe[j+1]){ 
                  flag=1; 
                  temp=total_scroe[j+1]; 
                  total_scroe[j+1]=total_scroe[j]; 
                  total_scroe[j]=temp; 
                  temp=chengji[j+1];
                  chengji[j+1]=chengji[j];
                  chengji[j]=temp;
               } 
               if(flag!=1)       //*******这个非常好
                break; 
        
     } 
    }
      

  2.   

     int temp,flag; 
    把这一行的,"temp,"去掉
      

  3.   

    前面的不对,下面这个应该可以:
    public static void bubble_sort(){//冒泡排序 
        int temp,flag; 
        int size=4; 
        int [] total_scroe=new int[4]; 
        String[] temp2;
        for(int i=0;i <=size-1;i++){ 
            total_scroe[i]=Integer.valueOf(chengji[i][5]).intValue(); 
        } 
        for(int i=0;i <3;i++){                          //将数据两两比较,将大的置于前 
            flag=0; 
            for(int j=0;j <size-i-1;j++) 
               if(total_scroe[j] <total_scroe[j+1]){ 
                  flag=1; 
                  temp=total_scroe[j+1]; 
                  total_scroe[j+1]=total_scroe[j]; 
                  total_scroe[j]=temp; 
                  temp2=chengji[j+1];
                  chengji[j+1]=chengji[j];
                  chengji[j]=temp2;
               } 
               if(flag!=1)       //*******这个非常好
                break; 
        
     } 
    }
      

  4.   

    public static void bubble_sort(){//冒泡排序 
        int temp,flag; 
        int size=4; 
        int [] total_scroe=new int[4]; 
        String[] temp2;
        for(int i=0;i <=size-1;i++){ 
            total_scroe[i]=Integer.valueOf(chengji[i][5]).intValue(); 
        } 
        for(int i=0;i <3;i++){                        
            flag=0; 
            for(int j=0;j <size-i-1;j++) 
               if(total_scroe[j] <total_scroe[j+1]){ 
                  flag=1; 
                  temp=total_scroe[j+1]; 
                  total_scroe[j+1]=total_scroe[j]; 
                  total_scroe[j]=temp; 
                  temp2=chengji[j+1];
                  chengji[j+1]=chengji[j];
                  chengji[j]=temp2;
               } 
               if(flag!=1)       
                break;   
     } 
    }
    可以的
      

  5.   

    for (int i = 0; i < 3; i++) { // 将数据两两比较,将大的置于前
    flag = 0;
    for (int j = 0; j < size - i - 1; j++)
    if (total_scroe[j] < total_scroe[j + 1]) {
     flag = 1;
    // temp = total_scroe[j + 1];
    // name = chengji[j + 1][1];
    // total_scroe[j + 1] = total_scroe[j];
    // chengji[j + 1][1] = chengji[j][1];
    // total_scroe[j] = temp;
    // chengji[j][1] = name;
    String[] tmp = chengji[j];
    chengji[j] = chengji[j + 1];
    chengji[j + 1] = tmp; }
    if (flag != 1)
    break; }是不是像上面直接交换一下引用就可以了
      

  6.   

    total_scroe[]也得跟着一起排序才行。不能只排chengji[]吧?
      

  7.   


    谢谢哈。。基础没学好不知道二维数组是可以用chengji[]表示整行。。那再问一个问题..如何进行整列表示???还有一个大问题....在INPUT方法中,名字输入中文的话..会出现乱码...该如何修改???
      

  8.   


    我6楼的话不是对你说的。
    按列排没有好的办法。当移动或交换列时,只能用一个循环把同一列的每一个元素都移动或交换。比较麻烦。
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in,"UTF-8")); 试试。这个不能确定。
      

  9.   


    for(int i = 0 ; i < 3; i++){
    for(int j = 0 ; j < 3 ; j++){
    for(int s = 0 ; s < 3-j-1;s++){
    if(Integer.parseInt(souce[i][s]) < Integer.parseInt(souce [i][s+1])){
    temp = souce[i][s];
    souce[i][s] = souce[i][s+1];
    souce[i][s+1] = temp;
                             }
    }
    }
    }
    for(int i = 0 ; i < 3 ; i++){
    for(int j = 0 ; j < 3 ; j++){
    System.out.println(souce[i][j]);
    }
    }
      

  10.   

    temp是在上面定义的一个String类型的变量,我这里是写死的数据,你可以参考这个方法