学生成绩管理系统(java实现)

来源:互联网 发布:允许淘宝访问麦克风 编辑:程序博客网 时间:2024/05/18 05:45

最近在看基础的java教程,发现java很多与c++类似,但也有不少差异,有很多要注意的地方,做了这个成绩管理系统,还没用到类,只是多维数组的应用。

期间遇到很多问题,也都经过一一百度解决了。

实现的功能: 输入学生人数,以及学生考试的科目数,然后依次填入信息,最后输出学生信息,以及总分,平均分,名次;

import java.util.*;import java.math.*;import java.text.*;//引入控制格式的包,import java.text.DecimalFormat; 用来控制浮点数小数位数public class Test{public static void main(String[] args){Scanner in=new Scanner(System.in);System.out.println("请输入一共有几门课程");int numberofCoarses=in.nextInt();System.out.println("请输入一共有几个学生");int numberofStudents=in.nextInt();System.out.println("请定义这几门课程");String [] coarse=new String[numberofCoarses];String [] name=new String[numberofStudents];for(int i=0;i<numberofCoarses;i++)coarse[i]=in.next();double[][] stuu =new double[numberofStudents][numberofCoarses];double[] totalGrades=new double [numberofStudents];double[] average=new double [numberofStudents];double[] totalsort=new double [numberofStudents];int[]  paihang = new int [numberofStudents];DecimalFormat df=new DecimalFormat("0.00");//设置浮点数的位数for(int i=0;i<numberofStudents;i++){System.out.println("请输入第"+(i+1)+"个学生的名字");name[i]=in.next();System.out.println("请依次输入"+name[i]+"的"+numberofCoarses+"门成绩");double sum=0;for(int j=0;j<numberofCoarses;j++){stuu[i][j]=in.nextDouble();sum+=stuu[i][j];}totalGrades[i]=sum;average[i]=sum/numberofCoarses;}//totalsort=totalGrades  错误,浅复制,指向同一个引用;//totalsort=totalGrades.clone();  //数组的深复制//用clone只能复制一维数组,多维数组需要加上for循环依次cloneSystem.arraycopy(totalGrades,0,totalsort,0,totalGrades.length);  //最快的赋值                                                                         //第一个0,代表源数组赋值的起始位置,第二个0,代表被赋值数组的起始位置 for(int i=0;i<totalsort.length-1;i++)   //选择排序{int k=i;for(int j=i+1;j<totalsort.length;j++){if(totalsort[j]>totalsort[k]){k=j;}}if(k!=i){double temp=totalsort[k];totalsort[k]=totalsort[i];totalsort[i]=temp;}} //Arrays.sort(totalsort,Collections.reverseOrder());  <<  有错误,改天再看看库函数怎么排序吧。。for(int i=0;i<paihang.length;i++)    //二分查找{int head=0,tail=paihang.length-1;while(head<=tail){if(totalsort[head]==totalGrades[i]){paihang[i]=head+1;break;  }else if(totalsort[tail]==totalGrades[i]){paihang[i]=tail+1;break;}else{int mid=(head+tail)/2;if(totalsort[mid]==totalGrades[i]){paihang[i]=mid+1;break;}else if(totalsort[mid]>totalGrades[i]){tail=mid-1;}elsehead=mid+1;}}}System.out.print("学生\t");for(int i=0;i<numberofCoarses;i++)System.out.print(coarse[i]+"\t");System.out.println("总分\t平均分\t排行榜");for(int i=0;i<numberofStudents;i++){System.out.print(name[i]+"\t");for(int j=0;j<stuu[i].length;j++){System.out.print(stuu[i][j]+"\t");}System.out.print(totalGrades[i]+"\t");System.out.print(df.format(average[i])+"\t");System.out.print("第"+paihang[i]+"名");System.out.println();}}}


1 0