专业: 计算机科学与技术 姓名:_ _ 学号:_ 日期:_2012-05-06_
课程名称 实验名称 指导教师
Java程序设计 化志章 实验室名称 学籍管理系统 成绩 X4313 1.实验目的
1、熟悉和掌握字符串、数组等两种高级数据类型;
2、进一步应用Scanner类的对象,读取不同类型的数据; 3、进一步训练自顶向下逐步求精策略,并应用于实际问题; 4、在函数的参数传递中,传递String对象和数组对象。 2.实验内容
设计的程序有如下功能:
1、能输入10个学生信息,学生信息中包含学号,姓名,年龄,语文,数学,英语等内容;
2、有统计功能,如统计个人的总分、平均分,以及整体的单科平均分、总分平均;
3、按语文、数学、英语、年龄、学号等中的任一种对学生进行排序; 4、能输出所有人的具体信息;
5、输入指定姓名或学号,输出符合条件的学生信息;
6、以循环菜单的方式列出上述功能供用户选择;输入0则退出系统.
注意:排序、统计、检索、打印等,必须用子程序单独实现,并以学生信息数组为参数。
7、数学、语文、英语均为成绩,考虑重用同一程序实施排序;
8、考虑系统的可交互性,即对要求用户输入信息格式必须给出友好的提示; 9、在完成相关功能后给出结果信息。 3。设计思路
1、用以下数组存储10位学生的信息:
public static int[] number=new int[10];//学号 public static String[] name=new String[10];//名字 public static int[] age=new int[10];//年龄 public static double[] chinese=new double[10];//语文 public static double[] math=new double[10];//数学 public static double[] english=new double[10];英语 public static double[] p_ave=new double[10];//平均分 public static double[] t_score=new double[10];//总分
2、对于功能(1:排序 2:统计 3:检索 4:打印) 分别用四个函数实现,具体算法见相应的代码;各函数对相应的数组进行操作。
3、最后根据用户的功能选择,主函数调用相应的函数实现其功能。
4.关键代码
import java.io。*; import java。util.*; public class Ad_student{ //全局变量 public static int[] number=new int[10]; public static String[] name=new String[10]; public static int[] age=new int[10]; public static double[] chinese=new double[10]; public static double[] math=new double[10]; public static double[] english=new double[10]; public static double[] p_ave=new double[10]; public static double[] t_score=new double[10]; public static double[] c_ave=new double[3]; public static double t_score_ave; public static void main(String args[]) throws IOException{ int i,n,m; System。out.println(\"******************学生管理系统************************”); System。out。println(\"请输入10个学生的信息及成绩(学号 姓名 语文 数学 英语)\"); Scanner s=new Scanner(System.in); for(i=0;i<10;i++) { number[i]=s。nextInt(); name[i]=s。next(); age[i]=s。nextInt(); chinese[i]=s.nextDouble(); math[i]=s。nextDouble(); english[i]=s。nextDouble(); } System。out.println(\"****************************************************\"); System。out.println(\"##主菜单## 1:排序 2:统计 3:检索 4:打印 0:退出 \"); System.out.println(”****************************************************”); System。out.println(\"请在主菜单中选择你要执行的操作代号:”);
while(true) { n=s.nextInt(); if(n==1) { sort(); } else if(n==2) { statistic1(); statistic2(); System.out.println(”信息统计完成,你可选择打印功能进行查看!\"); } else if(n==3) { search(); System.out.println(\"请在主菜单中选择你要执行的操作代号:\"); } else if(n==4) { statistic1(); statistic2(); print(); System.out。println(\"请在主菜单中选择你要执行的操作代号:”); } else { System.out。println(”谢谢你的使用!\"); break; } } } public static void sort() { int x; Scanner s2=new Scanner(System。in); System。out。println(”****************************************************”); System.out.println(” 1:语文 2:数学 3:英语 4:学号 5:年龄 ”); System。out。println(\"******************************
**********************”); System。out。println(\"请选择排序方式代号:\"); x=s2。nextInt(); for(int i=0;i〈9;i++) { int j,max; max=i; for(j=i+1;j〈10;j++) { if(x==4&&number[j]〉number[max]) max=j; else if(x==5&&age[j]>age[max]) max=j; else if(x==1&&chinese[j]〉chinese[max]) max=j; else if(x==2&&math[j]〉math[max]) max=j; else if(x==3&&english[j]>english[max]) max=j; } change(i,max); } System.out。println(\"已完成你选择的排序,你可选择打印功能进行查看!”); } public static void change(int i,int j) { String t1=name[i]; int t2=number[i]; int t3=age[i]; double t4=chinese[i]; double t5=math[i]; double t6=english[i]; double t7=p_ave[i]; double t8=t_score[i]; name[i]=name[j]; name[j]=t1; number[i]=number[j]; number[j]=t2; age[i]=age[j]; age[j]=t3;
chinese[i]=chinese[j]; chinese[j]=t4; math[i]=math[j]; math[j]=t5;
english[i]=english[j]; english[j]=t6;
p_ave[i]=p_ave[j]; p_ave[j]=t7;
t_score[i]=t_score[j]; t_score[j]=t8; }
//统计个人的总分,及平均分 public static void statistic1() {
int i;
for(i=0;i<10;i++) { t_score[i]=chinese[i]+math[i]+english[i]; p_ave[i]=t_score[i]/3; } }
//统计整体的单科平均分,及总分平均分 public static void statistic2() {
int i;
double sum1=0。0,sum2=0。0,sum3=0.0,sum4=0.0; for(i=0;i<10;i++) {
sum1+=chinese[i]; sum2+=math[i]; sum3+=english[i]; sum4+=t_score[i]; }
c_ave[0]=sum1/10;//语文平均分
c_ave[1]=sum2/10;//数学平均分 c_ave[2]=sum3/10;//英语平均分 t_score_ave=sum4/10;//总分平均分 }
public static void search() {
int x,i,j; String k;
Scanner s3=new Scanner(System.in);
System。out。println(\"请选择查询的方式代号(1:学号 2:姓名):\"); x=s3.nextInt(); if(x==1) { System。out.println(”请输入要查找的学生学号:\"); j=s3.nextInt(); for(i=0;i<10;i++) { if(j==number[i]) { System。out.printf(”%d %s %d %.1f %.1f %.1f\\n”,number[i],name[i],age[i],chinese[i],math[i],english[i]); break; } } if(i〉=10) System.out.println(”没找到你想查找的学生!”); } if(x==2) { System。out。println(\"请输入要查找的学生姓名:”); k=s3.next(); for(i=0;i〈10;i++) { if(k。equals(name[i])) { System.out.printf(”%d %s %d %。1f %.1f %.1f\\n”,number[i],name[i],age[i],chinese[i],math[i],english[i]); break; } } if(i〉=10) System。out.println(\"没找到你想查找的学生!\"); } } //打印所有信息 public static void print() { int i; System.out.println(”**************个人信息如下******************”); System。out.println(”学号 姓名 年龄 语文 数学 英语 总分 平均分\");
for(i=0;i〈10;i++) { System。out。printf(\"%d %s %d %。1f %。1f %。1f %。1f %。1f\\n”,number[i],name[i],age[i],chinese[i],math[i],english[i],t_score[i],p_ave[i]); } System。out。println(\"**************整体信息如下******************\"); System。out。println(”语文平均分”+c_ave[0]); System。out.println(”数学平均分\"+c_ave[1]); System.out.println(\"英语平均分\"+c_ave[2]); System。out。println(\"总分平均分\"+t_score_ave); } }
5.运行结果
检索功能运行结果截图:
统计功能运行结果截图:
按语文成绩从高到底排序的运行结果截图:
按语文成绩从高到底排序的运行结果截图:
6。实验总结(含心得体会) 通过编写此系统学到了:
1、能够应用字符串、数组等两种高级数据类型; 2、应用Scanner类的对象,读取不同类型的数据;
3、学会了在函数的参数传递中,传递String对象和数组对象; 4、会用java语言对数组中的元素进行排序;
因篇幅问题不能全部显示,请点此查看更多更全内容