闲暇之余用java写了个学生信息的管理系统,由于博主还是萌新,代码难免有冗余和错误的地方,如果您发现有什么不足之处或者错误,请留言。博主会尽量回复。
需求分析
一个学生信息管理系统应该包含这些内容:
教师页面
在教师页面应该包含:
学生页面
在教学生页面应该包含:
包含的类
根据分析,我们的学生信息系统应该包含三个类:
代码
学生类
student.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
package com.gcl;
import com.sun.org.glassfish.external.statistics.averagerangestatistic;
import java.util.scanner;
/**
* created by gcl on 2017/4/22.
*/
public class student
{
private int number; //学号
private string name; //姓名
private int age; //年龄
private double grade[]= new double [ 100 ]; //各科分数
private string project[]= new string[ 100 ]; //各科名称
private double average; //平均分
private int count; //科目数量
public void setnumber( int num)
{
this .number=num;
}
public int getnumber()
{
return number;
}
public void setname(string name)
{
this .name=name;
}
public string getname()
{
return name;
}
public void setage( int age)
{
this .age=age;
}
public int getage()
{
return age;
}
public void setgrade( double a[])
{
system.arraycopy(a, 0 , grade, 0 , a.length);
}
public double getgrade( int i)
{
return grade[i];
}
public void setproject(string a[])
{
system.arraycopy(a, 0 ,project, 0 ,a.length);
this .count=a.length;
}
public string getproject( int i)
{
return project[i];
}
public void setaverage( int len)
{
double sum= 0 ;
int i= 0 ;
for (;i<len;i++)
{
sum+=grade[i];
}
this .average=sum/(i* 1.0 );
}
public double getaverage()
{
return average;
}
public int getcount()
{
return count;
}
}
|
账号密码类
imfomation .java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.gcl;
/**
* created by gcl on 2017/5/6.
*/
public class imfomation {
private string code;
private string account;
public imfomation(string admin, string s) {
this .account=admin;
this .code=s;
}
public void setcode(string code)
{
this .code=code;
}
public string getcode()
{
return code;
}
public void setaccont(string account)
{
this .account=account;
}
public string getaccont()
{
return account;
}
}
|
系统操作类
main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
package com.gcl;
import java.util.objects;
import java.util.scanner;
/**
* created by gcl on 2017/5/7.
*/
public class main {
private static int m= 0 ; //科目数量
private static int n= 0 ; //学生数量
private static string[] a= new string[ 50 ];
private static imfomation ac= new imfomation( "admin" , "123456" ); //初始化教师账号密码
private static student su[]= new student[ 100 ]; //实例化学生类数组
public static void main(string[] args)
{
show(); //进入系统初始页面
}
//系统初始页面
private static void show()
{
system.out.println( "----------欢迎来到学生综合信息管理系统----------" );
system.out.println( "- 请先登录: -" );
system.out.println( "- 1.教师登录 -" );
system.out.println( "- 2.学生登录 -" );
system.out.println( "- 0.退出 -" );
system.out.println( "------------------------------------------------" );
scanner in= new scanner(system.in);
int ch=in.nextint();
if (ch== 1 ) login(ac);
else if (ch== 2 ) student();
else if (ch== 0 ) system.exit( 0 );
else system.exit( 1 );
}
//教师登录页面
private static void login(imfomation ac) //把ac当作参数传入
{
scanner in= new scanner(system.in);
system.out.println( "请输入账户名称:" );
string c=in.nextline();
if (!objects.equals(c, ac.getaccont()))
{
system.out.println( "账号不存在!" );
main.login(ac);
}
system.out.println( "请输入密码:" );
string b=in.nextline();
{
if (!objects.equals(b, ac.getcode()))
{
system.out.println( "密码错误!" );
main.login(ac);
}
}
system.out.println( "登录成功!" );
main.teacher();
}
//教师主页面
private static void teacher()
{
system.out.println( "-----------------------------------------------" );
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( "-----0.退出系统 ---------------" );
scanner ch= new scanner(system.in);
int c=ch.nextint();
if (c== 1 ) create();
else if (c== 2 ) search( 1 );
else if (c== 3 ) show();
else if (c== 4 ) delete();
else if (c== 5 ) renew();
else if (c== 6 ) recode();
else if (c== 0 ) show();
else
{
system.out.println( "输入错误!" );
teacher();
}
}
//学生主页面
private static void student()
{
system.out.println( "-----------------------------------------------" );
system.out.println( "-----请选择你要执行的操作: ---------------" );
system.out.println( "-----1.查询学生信息 ---------------" );
system.out.println( "-----0.退出系统 ---------------" );
scanner in= new scanner(system.in);
int ch=in.nextint();
if (ch== 1 ) search( 2 );
else if (ch== 0 ) show();
else
{
system.out.println( "输入错误!" );
student();
}
}
//创建学生信息
private static void create()
{
string str;
int num1= 0 ,t;
double [] g= new double [ 50 ];
while ( true )
{
su[n]= new student();
system.out.println( "请输入学生学号:" );
scanner in= new scanner(system.in);
num1=in.nextint();
for ( int i= 0 ;i<n;i++) //检测学号是否重复
{
while (su[i].getnumber() == num1)
{
system.out.println( "已经存在该学号,请重新输入:" );
num1=in.nextint();
}
}
su[n].setnumber(num1);
system.out.println( "请输入学生姓名:" );
str=in.next();
su[n].setname(str);
system.out.println( "请输入学生年龄:" );
t=in.nextint();
su[n].setage(t);
if (n== 0 ) //在创建第一个学生时,同时创建学科的数量和名称
{
system.out.println( "请输入n门课" );
m=in.nextint();
system.out.println( "请分别输入" +m+ "门科目的名字:" );
for ( int i= 0 ;i<m;i++)
{
a[i]=in.next();
}
}
su[n].setproject(a);
for ( int i= 0 ;i<m;i++)
{
system.out.println( "请输入" +su[n].getproject(i)+ "科目的成绩:" );
g[i]=in.nextint();
}
su[n].setgrade(g);
su[n].setaverage(m);
n++;
system.out.println( "是否继续添加?(是/y,否/n)" );
string cho=in.next();
char ch=cho.charat( 0 );
if (ch== 'n' ||ch== 'n' )
teacher(); //返回教师主页面
while (ch!= 'n' &&ch!= 'n' &&ch!= 'y' &&ch!= 'y' )
{
system.out.println( "输入无效,请重新输入:" );
cho=in.next();
ch=cho.charat( 0 );
}
}
}
//展示学生信息
private static void show()
{
system.out.println( "学生信息如下:" );
for ( int i= 0 ;i<n;i++)
{
system.out.println( "第" +i+ "名学生:" );
system.out.println( "学号:" +su[i].getnumber()+ "\t姓名:" +su[i].getname()+ "\t年龄:" +su[i].getage());
system.out.println( "该学生各科成绩为:" );
for ( int j= 0 ;j<m;j++)
{
system.out.println(su[i].getproject(j)+ ":\t" +su[i].getgrade(j));
}
system.out.println( "该学生平均分为:" +su[i].getaverage());
}
system.out.println( "输入任意值返回主菜单" );
scanner in= new scanner(system.in);
in.next();
teacher();
}
//搜索学生信息,参数是用来判断访问该函数的是学生还是老师
private static void search( int jud)
{
system.out.println( "----------------------" );
system.out.println( "----请选择搜索方式----" ); //选择搜索方式
system.out.println( "----1.按学号搜索 ----" );
system.out.println( "----2.按姓名搜索 ----" );
system.out.println( "----0.返回主菜单 ----" );
system.out.println( "----------------------" );
scanner in= new scanner(system.in);
int ch=in.nextint();
boolean temp= false ;
if (ch== 1 )
{
system.out.println( "请输入学号:" );
int nu=in.nextint();
for ( int i= 0 ;i<n;i++)
{
if (su[i].getnumber()==nu)
{
system.out.println( "寻找成功" );
system.out.println( "学号:" +su[i].getnumber()+ "\t姓名:" +su[i].getname()+ "\t年龄:" +su[i].getage());
system.out.println( "该学生各科成绩为:" );
for ( int j= 0 ;j<m;j++)
{
system.out.println(su[i].getproject(j)+ ":\t" +su[i].getgrade(j));
}
system.out.println( "该学生平均分为:" +su[i].getaverage());
system.out.println( "输入任意值返回主菜单" );
scanner in= new scanner(system.in);
in.next();
if (jud== 1 ) //如果是老师访问,返回teacher()
teacher();
student(); //否则返回student()
}
}
system.out.println( "没有找到该学生,即将返回" );
if (jud== 1 )
teacher();
student();
}
else if (ch== 2 )
{
system.out.println( "请输入姓名:" );
string na=in.next();
for ( int i= 0 ;i<n;i++)
{
if (objects.equals(su[i].getname(), na))
{
system.out.println( "寻找成功" );
system.out.println( "学号:" +su[i].getnumber()+ "\t姓名:" +su[i].getname()+ "\t年龄:" +su[i].getage());
system.out.println( "该学生各科成绩为:" );
for ( int j= 0 ;j<m;j++)
{
system.out.println(su[i].getproject(j)+ ":\t" +su[i].getgrade(j));
}
system.out.println( "该学生平均分为:" +su[i].getaverage());
temp= true ;
}
}
if (temp)
{
system.out.println( "输入任意值返回主菜单" );
scanner in= new scanner(system.in);
in.next();
if (jud== 1 )
teacher();
student();
}
else
{
system.out.println( "没有找到该学生,即将返回" );
if (jud== 1 )
teacher();
student();
}
}
else if (ch== 0 )
{
if (jud== 1 )
teacher();
student();
}
else
{
system.out.println( "输入错误!" );
search(jud);
}
}
//删除学生信息
private static void delete()
{
system.out.println( "请输入要删除的学生的学号:" );
scanner in= new scanner(system.in);
int num=in.nextint();
boolean temp= true ; //判断是否找到该学生
int tem= 0 ,i= 0 ;
for (i= 0 ;i<n;i++)
{
if (su[i].getnumber()==num)
{
temp= false ;
tem=i; //用tem记录该学生的位置
break ;
}
}
if (temp)
{
system.out.println( "未找到该学生信息" );
system.out.println( "输入任意值返回主菜单" );
scanner in= new scanner(system.in);
in.next();
teacher();
}
else
{
system.out.println( "你要删除的学生信息:" );
system.out.println( "学号:" +su[tem].getnumber()+ "\t姓名:" +su[tem].getname()+ "\t年龄:" +su[tem].getage());
system.out.println( "该学生各科成绩为:" );
for ( int j= 0 ;j<m;j++)
{
system.out.println(su[tem].getproject(j)+ ":\t" +su[tem].getgrade(j));
}
system.out.println( "该学生平均分为:" +su[tem].getaverage());
system.out.println( "你确定要删除该学生吗?(是/y,否/n)" );
string ch=in.next();
char se=ch.charat( 0 );
while (se!= 'n' &&se!= 'n' &&se!= 'y' &&se!= 'y' )
{
system.out.println( "输入无效,请重新输入:" );
ch=in.next();
se=ch.charat( 0 );
}
if (se== 'n' ||se== 'n' ) {
system.out.println( "返回主菜单" );
teacher();
}
if (se== 'y' ||se== 'y' ) //通过线性表的删除操作来删除该学生的信息
{
for (i=tem;i<n- 1 ;i++)
{
su[i]=su[i+ 1 ];
}
n--;
system.out.println( "学生信息删除成功!" );
system.out.println( "输入任意值返回主菜单" );
scanner in= new scanner(system.in);
in.next();
teacher();
}
}
}
//修改学生信息
private static void renew()
{
system.out.println( "请输入要修改的学生的学号:" );
scanner in= new scanner(system.in);
int num=in.nextint();
boolean temp= true ;
int tem= 0 ,i= 0 ;
for (i= 0 ;i<n;i++)
{
if (su[i].getnumber()==num)
{
temp= false ;
tem=i;
break ;
}
}
if (temp)
{
system.out.println( "未找到该学生信息" );
system.out.println( "输入任意值返回主菜单" );
scanner in= new scanner(system.in);
in.next();
teacher();
}
else
{
system.out.println( "你要修改的学生信息如下:" );
system.out.println( "你要修改的学生信息:" );
system.out.println( "学号:" +su[tem].getnumber()+ "\t姓名:" +su[tem].getname()+ "\t年龄:" +su[tem].getage());
system.out.println( "该学生各科成绩为:" );
for ( int j= 0 ;j<m;j++)
{
system.out.println(su[tem].getproject(j)+ ":\t" +su[tem].getgrade(j));
}
system.out.println( "该学生平均分为:" +su[tem].getaverage());
system.out.println( "你确定要修改该学生吗?(是/y,否/n)" );
string ch=in.next();
char se=ch.charat( 0 );
while (se!= 'n' &&se!= 'n' &&se!= 'y' &&se!= 'y' )
{
system.out.println( "输入无效,请重新输入:" );
ch=in.next();
se=ch.charat( 0 );
}
if (se== 'n' ||se== 'n' )
{
system.out.println( "返回主菜单" );
teacher();
}
if (se== 'y' ||se== 'y' )
{
double [] g= new double [ 50 ];
system.out.println( "请选择修改的内容:" ); //选择修改哪些内容
system.out.println( "---1.修改姓名----" );
system.out.println( "---2.修改年龄----" );
system.out.println( "---3.修改成绩----" );
system.out.println( "---0.返回主菜单--" );
int cho=in.nextint();
if (cho== 1 )
{
system.out.println( "请输入姓名:" );
string name=in.next();
su[tem].setname(name);
system.out.println( "修改成功!" );
system.out.println( "还要继续修改吗?(是/y,否/n)" );
ch=in.next();
se=ch.charat( 0 );
while (se!= 'n' &&se!= 'n' &&se!= 'y' &&se!= 'y' )
{
system.out.println( "输入无效,请重新输入:" );
ch=in.next();
se=ch.charat( 0 );
}
if (se== 'n' ||se== 'n' )
{
system.out.println( "返回主菜单" );
teacher();
}
if (se== 'y' ||se== 'y' )
{
renew();
}
}
else if (cho== 2 )
{
system.out.println( "请输入年龄:" );
int ag=in.nextint();
su[tem].setage(ag);
system.out.println( "修改成功!" );
system.out.println( "还要继续修改吗?(是/y,否/n)" );
ch=in.next();
se=ch.charat( 0 );
while (se!= 'n' &&se!= 'n' &&se!= 'y' &&se!= 'y' )
{
system.out.println( "输入无效,请重新输入:" );
ch=in.next();
se=ch.charat( 0 );
}
if (se== 'n' ||se== 'n' )
{
system.out.println( "返回主菜单" );
teacher();
}
if (se== 'y' ||se== 'y' )
{
renew();
}
}
else if (cho== 3 )
{
for ( int j= 0 ;j<m;j++)
{
system.out.println( "请输入" +su[tem].getproject(j)+ "科目的成绩:" );
g[j]=in.nextint();
}
su[tem].setgrade(g);
su[tem].setaverage(m);
system.out.println( "修改成功!" );
system.out.println( "还要继续修改吗?(是/y,否/n)" );
ch=in.next();
se=ch.charat( 0 );
while (se!= 'n' &&se!= 'n' &&se!= 'y' &&se!= 'y' )
{
system.out.println( "输入无效,请重新输入:" );
ch=in.next();
se=ch.charat( 0 );
}
if (se== 'n' ||se== 'n' )
{
system.out.println( "返回主菜单" );
teacher();
}
if (se== 'y' ||se== 'y' )
{
renew();
}
}
else if (cho== 0 )
{
teacher();
}
else
{
system.out.println( "输入无效!" );
renew();
}
}
}
}
//修改教师的密码
private static void recode()
{
system.out.println( "请输入原来的密码:" );
scanner in= new scanner(system.in);
string code =in.nextline();
if (!objects.equals(code, ac.getcode()))
{
system.out.println( "密码输入错误!" );
teacher();
}
system.out.println( "请输入新密码:" ); //要求输入两次新密码
string co1=in.nextline();
system.out.println( "请再次输入新密码:" );
string co2=in.nextline();
if (!objects.equals(co1, co2))
{
system.out.println( "两次输入密码不一致!" );
system.out.println( "即将返回主界面" );
teacher();
}
else
{
ac.setcode(co1);
system.out.println( "密码修改成功!" );
system.out.println( "输入任意值返回主菜单" );
scanner in= new scanner(system.in);
in.next();
teacher();
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/PYLON_GCL/article/details/71433634