学生成绩管理系统
项目要求:
- 需要实现9种功能
- 录入初始数据
- 插入新同学信息
- 删除
- 总人数
- 按学号查找
- 按姓名查找
- 成绩排序
- 显示所有学生信息
- 退出
- 功能选择需要通过控制面板实现
- 只能通过控制面板选项退出程序
- 线性表相关操作需要封装成方法
代码框架搭建:
实现功能需要选择
需要使用选择的流程控制语句,9种选择用switch语句实现
程序不能自动退出,只能通过选项实现
所以,控制面板和switch语句是在一个循环里的,并且“退出”功能能够跳出这个循环,结束程序.
总结:
用while语句将控制面板和switch语句包含,并且设置boolean类型的变量作为while的循环条件,只有”退出“功能能够修改。
9种方法分别分隔到switch语句的9个case里,逐一实现。
框架模拟:
1 | boolean flag = true; |
代码编译思想:
本次分别运用了两种思想写了两个版本的代码,分别是:标准版和Plus版
标准版运用的是面向过程的思想
Plus版运用的是面向过程的思想
代码实现:
标准版
1 | import java.util.Scanner; |
Plus版:
main方法:
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
61import java.util.Scanner;
public class Student_Achievement_Management_System_Plus {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String selection;//声明用户选项
SqList student = new SqList();//声明线性表
boolean flag = true;//声明并初始化while语句的控制条件
while(flag) {
//输出控制面板
Utility.Print_control_panel();
//获得用户选项
selection = scan.next();
//加一个输入保护功能
selection = Utility.Print_protect(selection);
//9大功能模块
switch(selection) {
//创建线性表
case "1":
student.InitList();//初始化线性表
//获取新同学插入位置、学号、姓名、成绩信息
Utility.Check_in_Information(student);
break;
//插入新同学信息
case "2":
Utility.Insert_new_information(student);
break;
//删除
case "3":
Utility.Delet_information(student);
break;
//总人数
case "4":
Utility.Length(student);
break;
//按学号查找
case "5":
Utility.Location_num(student);
break;
//按姓名查找
case "6":
Utility.Location_name(student);
break;
//成绩排序
case "7":
Utility.Sort_score(student);
break;
//显示所有学生信息
case "8":
Utility.Print_all(student);
break;
//退出
case "9":
flag = Utility.Exite(flag);
break;
}
}
scan.close();
}
}Utility工具类:
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
165package student_achievement_management_system_plus;
import java.util.Scanner;
public class Utility {
static Scanner scan = new Scanner(System.in);
//输出控制面板
public static void Print_control_panel() {
System.out.println("\t\t1---录入初始数据");
System.out.println("\t\t2---插入新同学信息");
System.out.println("\t\t3---删除");
System.out.println("\t\t4---输出总人数");
System.out.println("\t\t5---按学号查找");
System.out.println("\t\t6---按姓名查找");
System.out.println("\t\t7---按成绩排序");
System.out.println("\t\t8---显示所有学生信息");
System.out.println("\t\t9---退出");
System.out.print("请选择功能:");
}
//输入保护功能
public static String Print_protect(String e) {
char chr = e.charAt(0);
while(chr > 49 && chr > 57) {
System.out.print("选择错误,请重新输入:");
e = scan.next();
chr = e.charAt(0);
}
return e;
}
//输入线性表初始信息
public static void Check_in_Information(SqList L) {
System.out.println("请输入学生成绩,当前学号为#时结束\n");
int i = 0;
while(true) {
System.out.print("请输入学号:");
L.num[i] = scan.next();
//设置结束循环的逻辑
char num_flag = L.num[i].charAt(0);
if(num_flag == '#') {
L.Clean();//暴力清屏逻辑
break;
}
System.out.print("请输入姓名:");
L.name[i] = scan.next();
System.out.print("请输入成绩:");
L.score[i] = scan.nextInt();
L.length ++;
i ++;
}
}
//插入新同学信息
public static void Insert_new_information(SqList L) {
//获取新同学插入位置、学号、姓名、成绩信息
System.out.print("请输入插入位置:");
int location = scan.nextInt();
System.out.print("请输入学号:");
String num = scan.next();
System.out.print("请输入姓名:");
String name = scan.next();
System.out.print("请输入成绩:");
int score = scan.nextInt();
//将新同学的信息插入线性表
L.ListInsert(location, num, name, score);
//暴力清屏逻辑
Clean(L);
}
//删除学生信息
public static void Delet_information(SqList L) {
System.out.print("请输入删除的位置:");
int location = scan.nextInt();
L.ListDelete(location);
L.length --;//线性表长度减一
//暴力清屏逻辑
Clean(L);
}
//总人数
public static void Length(SqList L) {
System.out.println("学生总数为:" + L.ListLength());
//暴力清屏逻辑
Clean(L);
}
//按学号查找
public static void Location_num(SqList L) {
System.out.print("请输入查找学生的学号:");
String num = scan.next();
int Location = L.LocationElem_num(num) - 1;
System.out.println("查找到学生信息如下:");
System.out.println("学号\t\t姓名\t\t成绩" );
System.out.println(L.num[Location] + "\t"+ L.name[Location] + "\t\t" + L.score[Location]);
//暴力清屏逻辑
Clean(L);
}
//按姓名查找
public static void Location_name(SqList L) {
System.out.print("请输入查找学生的姓名:");
String name = scan.next();
int Location = L.LocationElem_name(name) - 1;
System.out.println("查找到学生信息如下:");
System.out.println("学号\t\t姓名\t\t成绩" );
System.out.println(L.num[Location] + "\t"+ L.name[Location] + "\t\t" + L.score[Location]);
//暴力清屏逻辑
Clean(L);
}
//按成绩排序
public static void Sort_score(SqList L) {
System.out.println("将成绩从高到低排序");
int length = L.ListLength();
for(int m = 0 ; m < length - 1; m++) {
//循环(length-1)次
for(int n = 0; n < length - 1 - m; n ++) {
if(L.score[n] < L.score[n+1]) {
//将小的score[]前移一位
Array_displace(n, L.score);
//将name[]同步变换
Array_displace(n, L.name);
//将num[]同步变换
Array_displace(n, L.num);
}
}
}
//暴力清屏逻辑
Clean(L);
}
//显示所有学生的信息
public static void Print_all(SqList L) {
System.out.println("学号\t\t\t姓名\t\t成绩" );
for(int i = 0; i <= L.length - 1; i ++ ) {
System.out.println(L.num[i] + "\t\t"+ L.name[i] + "\t\t" + L.score[i]);
}
//暴力清屏逻辑
Clean(L);
}
//退出
public static boolean Exite(boolean flag) {
System.out.println("您确定要退出吗?(Y/N)");
String exit = scan.next();
char exit_chr = exit.charAt(0);
if(exit_chr == 'Y') {
flag = false;
}
return flag;
}
//暴力清屏逻辑
public static void Clean(SqList L) {
System.out.print("是否进行清屏操作(Y/Others):");
String flag2 = scan.next();
char flag_chr2 = flag2.charAt(0);
if(flag_chr2 == 'Y') {
L.Clean();
}
}
//String型数组前移一位
public static void Array_displace(int n, String[] array ) {
String temp1 = array[n];
array[n] = array[n+1];
array[n+1] = temp1;
}
//int型数组前移一位(这里运用到了方法的重载)
public static void Array_displace(int n, int[] array ) {
int temp1 = array[n];
array[n] = array[n+1];
array[n+1] = temp1;
}
}线性表类:
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
96class SqList<E> {
//属性:
final int MAXSIZE = 100;
String[] num = new String[MAXSIZE];
String[] name = new String[MAXSIZE];
int[] score = new int[MAXSIZE];
int length;
//方法
//初始化线性表
public void InitList() {
this.length = 0;
}
//插入新元素
public boolean ListInsert(int i, String e1, String e2, int e3) {
int k;
//判断线性表是否已满
if(this.length == MAXSIZE)
return false;
//判断i是否有效
if(i < 1 || i > this.length + 1)
return false;
if(i <= this.length) {
//将第i个位置及其之后的元素后移一位
for(k = this.length-1; k >= i-1; k--)
{
this.num[k+1] = this.num[k];
this.name[k+1] = this.name[k];
this.score[k+1] = this.score[k];
}
}
//将新元素插入
this.num[i-1] = e1;
this.name[i-1] = e2;
this.score[i-1] = e3;
//更新线性表的长度
this.length ++;
return true;
}
//删除第i个元素
public boolean ListDelete(int i) {
int k;
//判断方法是否有意义
if(this.length == 0 || i < 1 || i > this.length)
return false;
//删除第i个元素,并将后面的元素前移
for(k = i; k < this.length; k++)
{
this.num[k-1] = this.num[k];
this.name[k-1] = this.name[k];
this.score[k-1] = this.score[k];
}
return true;
}
//获取线性表长度
public int ListLength() {
return this.length;
}
//查找线性表中是否有某元素,并返回其索引(按学号查找)
public int LocationElem_num(String e) {
int i;
//判断线性表是否为空
if(this.length == 0)
return 0;
//在线性表中寻找数据元素e,并获取其位置
for(i = 0; i < this.length; i++)
{
if(this.num[i].equals(e)) {
return (i+1);
}
}
//返回数据元素e在线性表的位置
return 0;
}
//查找线性表中是否有某元素,并返回其索引(按姓名查找)
public int LocationElem_name(String e) {
int i;
//判断线性表是否为空
if(this.length == 0)
return 0;
//在线性表中寻找数据元素e,并获取其位置
for(i = 0; i < this.length; i++)
{
if(this.name[i].equals(e)) {
return (i + 1);
}
}
//返回数据元素e在线性表的位置
return 0;
}
//暴力清屏逻辑
public void Clean() {
for(int i = 0; i <= 30; i ++) {
System.out.println("");
}
}
}
检测运行
标准版和Plus版的运行结果都是一样的。
录入初始数据:
插入新同学信息:
显示所有学生信息:
按成绩排序:
显示所有学生信息:
删除:
显示所有学生信息:
输出总人数:
按学号查找:
按姓名查找:
退出:
代码自评:
标准版和Plus版哪个好?
首先声明,之所以命名为Plus版和标准版并不是因为面向对象比面向过程好,单纯只是因为我是先写的面向过程,后写的面向对象,先标准后Plus算是我的个人习惯。
运行效率上两个差不多,编写难度看个人情况,哪个用的顺手哪个就容易(我写Plus比较快),代码量上Plus还要多上一丢丢(面向对象搭框架比较多)。总体评价标准版要略胜一筹,也就是面型过程要比面向对象强一点。
这个项目用面向过程更好,为什么我写了标准版还要再写个Plus版本的呢?
这个主要还是因为我主要学的是Java,以后肯定是要用面向对象的。但是和面向过程相比,面向对象再千行以上才能有较为明显的优势。然而在大学期间,基本不太可能经常接触千行量级的代码,难道就因为这个一直不用面向对象吗?
很显然不可能,所以在200行以上级别的代码,我都会尝试使用面向对象的思想设计程序。
*该评价很多观点的详细情况来自我的一篇博客Java面向对象之面向对象的理解,有兴趣的可以跳转过去看一下。
代码评价:
总体评价:
比较满意,遗憾很多。
比较满意是因为总体来说大方向上做的都挺好,无论是框架搭建,还是代码逻辑设计都完成得很不错,并且收获不小。
遗憾的是由于几个方面的原因,导致这两个程序其实还有着不少的缺陷。
遗憾:
知识储备不足:
由于我是不准备考研的,那么我就必须在技术上站得住脚,再加上我选的就是Java后端开发这条路,所以,我在Java底层和实践上钻的比较深。钻的比较深就意味着费时间,学得慢。所以我实际上面向对象只学了很少一部分,类及其成员只学了方法和属性,面向对象的特性只有大概的了解,关键字几乎一个没学。
这就导致这两个程序在很多细节上极其不完善,比如:构造器,private等我都没用(不是不会用,只是不熟练,索性就直接不用了——小声辩解)
也没办法,目前只能做到这一步。
时间耗不起:
其实在我的想法里,还要再加上很多保护机制,以防止用户乱输入导致程序出错。这一点其实挺重要的,但是最后我还是没弄。主要原因就是没时间。
保护机制和其他一些没加上的细节都没啥技术含量而且需要各种调试,十分的浪费时间。再加上本身我是特别缺时间的,前面也说了,我的知识储备还是很欠缺的,时间很紧张。花费大量时间到这些不痛不痒的事情上,实在有些得不偿失。
基于以上种种原因,我还是放弃了为程序增添一些细节上的东西。
所以说挺遗憾的,但也没办法,成年人的世界不能像小孩子那样任性地说“我全都要”。有时候该舍弃就得舍弃,跑得越快损失越小。
(不知不觉,我也快20岁了,19岁和20岁,感觉明显不一样了)
Plus+版:
Main方法类:
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
package student_achievement_management_system_plus;
import java.util.Scanner;
public class Student_Achievement_Management_System_Plus {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String selection;//声明用户选项
SqList student = new SqList();//声明线性表
boolean flag = true;//声明并初始化while语句的控制条件
while(flag) {
//输出控制面板
Utility.Print_control_panel();
//获得用户选项
selection = scan.next();
//加一个输入保护功能
selection = Utility.Print_protect(selection);
//9大功能模块
switch(selection) {
//创建线性表
case "1":
student.InitList();//初始化线性表
//获取新同学插入位置、学号、姓名、成绩信息
Utility.Check_in_Information(student);
break;
//插入新同学信息
case "2":
Utility.Insert_new_information(student);
break;
//删除
case "3":
Utility.Delet_information(student);
break;
//总人数
case "4":
Utility.Length(student);
break;
//按学号查找
case "5":
Utility.Location_num(student);
break;
//按姓名查找
case "6":
Utility.Location_name(student);
break;
//成绩排序
case "7":
Utility.Sort_score(student);
break;
//显示所有学生信息
case "8":
Utility.Print_all(student);
break;
//退出
case "9":
flag = Utility.Exite(flag);
break;
}
}
scan.close();
}
}Utility类:
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
140package student_achievement_management_system_plus;
import java.util.Scanner;
class Utility {
static Scanner scan = new Scanner(System.in);
//输出控制面板
static void Print_control_panel() {
System.out.println("\t\t1---录入初始数据");
System.out.println("\t\t2---插入新同学信息");
System.out.println("\t\t3---删除");
System.out.println("\t\t4---输出总人数");
System.out.println("\t\t5---按学号查找");
System.out.println("\t\t6---按姓名查找");
System.out.println("\t\t7---按成绩排序");
System.out.println("\t\t8---显示所有学生信息");
System.out.println("\t\t9---退出");
System.out.print("请选择功能:");
}
//输入保护功能
static String Print_protect(String e) {
char chr = e.charAt(0);
while(chr > 49 && chr > 57) {
System.out.print("选择错误,请重新输入:");
e = scan.next();
chr = e.charAt(0);
}
return e;
}
//输入线性表初始信息
static void Check_in_Information(SqList L) {
System.out.println("请输入学生成绩,当前学号为#时结束\n");
int i = 0;
while(true) {
System.out.print("请输入学号:");
L.setNum(i, scan.next());
//设置结束循环的逻辑
char num_flag = L.getNum(i).charAt(0);
if(num_flag == '#') {
L.Clean();//暴力清屏逻辑
break;
}
System.out.print("请输入姓名:");
L.setName(i, scan.next());
System.out.print("请输入成绩:");
L.setScore(i, scan.nextInt());
L.setLength(1);
i ++;
}
}
//插入新同学信息
static void Insert_new_information(SqList L) {
//获取新同学插入位置、学号、姓名、成绩信息
System.out.print("请输入插入位置:");
int location = scan.nextInt();
System.out.print("请输入学号:");
String num = scan.next();
System.out.print("请输入姓名:");
String name = scan.next();
System.out.print("请输入成绩:");
int score = scan.nextInt();
//将新同学的信息插入线性表
L.ListInsert(location, num, name, score);
//暴力清屏逻辑
Clean(L);
}
//删除学生信息
static void Delet_information(SqList L) {
System.out.print("请输入删除的位置:");
int location = scan.nextInt();
L.ListDelete(location);
L.setLength(-1);//线性表长度减一
//暴力清屏逻辑
Clean(L);
}
//总人数
static void Length(SqList L) {
System.out.println("学生总数为:" + L.ListLength());
//暴力清屏逻辑
Clean(L);
}
//按学号查找
static void Location_num(SqList L) {
System.out.print("请输入查找学生的学号:");
String num = scan.next();
int Location = L.LocationElem_num(num) - 1;
System.out.println("查找到学生信息如下:");
System.out.println("学号\t\t姓名\t\t成绩" );
System.out.println(L.getNum(Location) + "\t"+ L.getName(Location) + "\t\t" + L.getScore(Location));
//暴力清屏逻辑
Clean(L);
}
//按姓名查找
static void Location_name(SqList L) {
System.out.print("请输入查找学生的姓名:");
String name = scan.next();
int Location = L.LocationElem_name(name) - 1;
System.out.println("查找到学生信息如下:");
System.out.println("学号\t\t姓名\t\t成绩" );
System.out.println(L.getNum(Location) + "\t"+ L.getName(Location) + "\t\t" + L.getScore(Location));
//暴力清屏逻辑
Clean(L);
}
//按成绩排序
static void Sort_score(SqList L) {
System.out.println("将成绩从高到低排序");
L.Sort_high();
//暴力清屏逻辑
Clean(L);
}
//显示所有学生的信息
static void Print_all(SqList L) {
System.out.println("学号\t\t\t姓名\t\t成绩" );
for(int i = 0; i <= L.getLength() - 1; i ++ ) {
System.out.println(L.getNum(i) + "\t\t"+ L.getName(i) + "\t\t" + L.getScore(i));
}
//暴力清屏逻辑
Clean(L);
}
//退出
static boolean Exite(boolean flag) {
System.out.println("您确定要退出吗?(Y/N)");
String exit = scan.next();
char exit_chr = exit.charAt(0);
if(exit_chr == 'Y') {
flag = false;
}
return flag;
}
//暴力清屏逻辑
private static void Clean(SqList L) {
System.out.print("是否进行清屏操作(Y/Others):");
String flag2 = scan.next();
char flag_chr2 = flag2.charAt(0);
if(flag_chr2 == 'Y') {
L.Clean();
}
}
}SqList类:
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
154package student_achievement_management_system_plus;
//线性表结构
class SqList<E> {
//属性:
private final int MAXSIZE = 100;
private String[] num = new String[MAXSIZE];
private String[] name = new String[MAXSIZE];
private int[] score = new int[MAXSIZE];
private int length;
//方法
//初始化线性表
void InitList() {
this.length = 0;
}
//获取属性的值
String getNum(int i) {
return num[i];
}
String getName(int i) {
return name[i];
}
int getScore(int i) {
return score[i];
}
int getLength() {
return length;
}
//设置属性的值
void setNum(int i, String e) {
this.num[i] = e;
}
void setName(int i, String e) {
this.name[i] = e;
}
void setScore(int i, int e) {
this.score[i] = e;
}
void setLength(int e) {
this.length = length + e;
}
//插入新元素
boolean ListInsert(int i, String e1, String e2, int e3) {
int k;
//判断线性表是否已满
if(this.length == MAXSIZE)
return false;
//判断i是否有效
if(i < 1 || i > this.length + 1)
return false;
if(i <= this.length) {
//将第i个位置及其之后的元素后移一位
for(k = this.length-1; k >= i-1; k--)
{
this.num[k+1] = this.num[k];
this.name[k+1] = this.name[k];
this.score[k+1] = this.score[k];
}
}
//将新元素插入
this.num[i-1] = e1;
this.name[i-1] = e2;
this.score[i-1] = e3;
//更新线性表的长度
this.length ++;
return true;
}
//删除第i个元素
boolean ListDelete(int i) {
int k;
//判断方法是否有意义
if(this.length == 0 || i < 1 || i > this.length)
return false;
//删除第i个元素,并将后面的元素前移
for(k = i; k < this.length; k++)
{
this.num[k-1] = this.num[k];
this.name[k-1] = this.name[k];
this.score[k-1] = this.score[k];
}
return true;
}
//获取线性表长度
int ListLength() {
return this.length;
}
//查找线性表中是否有某元素,并返回其索引(按学号查找)
int LocationElem_num(String e) {
int i;
//判断线性表是否为空
if(this.length == 0)
return 0;
//在线性表中寻找数据元素e,并获取其位置
for(i = 0; i < this.length; i++)
{
if(this.num[i].equals(e)) {
return (i+1);
}
}
//返回数据元素e在线性表的位置
return 0;
}
//查找线性表中是否有某元素,并返回其索引(按姓名查找)
int LocationElem_name(String e) {
int i;
//判断线性表是否为空
if(this.length == 0)
return 0;
//在线性表中寻找数据元素e,并获取其位置
for(i = 0; i < this.length; i++)
{
if(this.name[i].contentEquals(e)) {
return (i + 1);
}
}
//返回数据元素e在线性表的位置
return 0;
}
//将成绩从高到低进行排序
void Sort_high() {
int length = this.ListLength();
for(int m = 0 ; m < length - 1; m++) {
//循环(length-1)次
for(int n = 0; n < length - 1 - m; n ++) {
if(this.score[n] < this.score[n+1]) {
//将小的score[]前移一位
Array_displace(n, this.score);
//将name[]同步变换
Array_displace(n, this.name);
//将num[]同步变换
Array_displace(n, this.num);
}
}
}
}
//暴力清屏逻辑
void Clean() {
for(int i = 0; i <= 30; i ++) {
System.out.println("");
}
}
//String型数组前移一位
private static void Array_displace(int n, String[] array ) {
String temp1 = array[n];
array[n] = array[n+1];
array[n+1] = temp1;
}
//int型数组前移一位(这里运用到了方法的重载)
private static void Array_displace(int n, int[] array ) {
int temp1 = array[n];
array[n] = array[n+1];
array[n+1] = temp1;
}
}
单链表版:
Main方法类:
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
63package view.ui;
import java.util.Scanner;
import model.bean.*;
import controller.service.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String selection;//声明用户选项
LinkList student = new LinkList();//声明线性表
boolean flag = true;//声明并初始化while语句的控制条件
while(flag) {
//输出控制面板
Utility.Print_control_panel();
//获得用户选项
selection = scan.next();
//加一个输入保护功能
selection = Utility.Print_protect(selection);
//9大功能模块
switch(selection) {
//创建线性表
case "1":
//获取新同学插入位置、学号、姓名、成绩信息
Utility.Check_in_Information(student);
break;
//插入新同学信息
case "2":
Utility.Insert_new_information(student);
break;
//删除
case "3":
Utility.Delet_information(student);
break;
//总人数
case "4":
Utility.Length(student);
break;
//按学号查找
case "5":
Utility.Location_num(student);
break;
//按姓名查找
case "6":
Utility.Location_name(student);
break;
//成绩排序
case "7":
Utility.Sort_score(student);
break;
//显示所有学生信息
case "8":
Utility.Print_all(student);
break;
//退出
case "9":
flag = Utility.Exite(flag);
break;
}
}
scan.close();
}
}Utility类:
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
160package controller.service;
import java.util.Scanner;
import model.bean.*;
public class Utility {
static Scanner scan = new Scanner(System.in);
//输出控制面板
public static void Print_control_panel() {
System.out.println("\t\t1---录入初始数据");
System.out.println("\t\t2---插入新同学信息");
System.out.println("\t\t3---删除");
System.out.println("\t\t4---输出总人数");
System.out.println("\t\t5---按学号查找");
System.out.println("\t\t6---按姓名查找");
System.out.println("\t\t7---按成绩排序");
System.out.println("\t\t8---显示所有学生信息");
System.out.println("\t\t9---退出");
System.out.print("请选择功能:");
}
//输入保护功能
public static String Print_protect(String e) {
char chr = e.charAt(0);
while(chr > 49 && chr > 57) {
System.out.print("选择错误,请重新输入:");
e = scan.next();
chr = e.charAt(0);
}
return e;
}
//输入线性表初始信息
public static void Check_in_Information(LinkList L) {
System.out.println("请输入学生成绩,当前学号为#时结束\n");
String num;
String name;
int score;
while(true) {
System.out.print("请输入学号:");
num = scan.next();
//设置结束循环的逻辑
char num_flag = num.charAt(0);
if(num_flag == '#') {
L.Clean();//暴力清屏逻辑
break;
}
System.out.print("请输入姓名:");
name = scan.next();
System.out.print("请输入成绩:");
score = scan.nextInt();
L.add( num, name, score);
}
}
//插入新同学信息
public static void Insert_new_information(LinkList L) {
//获取新同学插入位置、学号、姓名、成绩信息
System.out.print("请输入插入位置:");
int location = scan.nextInt();
System.out.print("请输入学号:");
String num = scan.next();
System.out.print("请输入姓名:");
String name = scan.next();
System.out.print("请输入成绩:");
int score = scan.nextInt();
//将新同学的信息插入线性表
L.ListInsert(location, num, name, score);
//暴力清屏逻辑
Clean(L);
}
//删除学生信息
public static void Delet_information(LinkList L) {
System.out.print("请输入删除的位置:");
int location = scan.nextInt();
L.ListDelete(location);
//暴力清屏逻辑
Clean(L);
}
//总人数
public static void Length(LinkList L) {
System.out.println("学生总数为:" + L.Length());
//暴力清屏逻辑
Clean(L);
}
//按学号查找
public static void Location_num(LinkList L) {
System.out.print("请输入查找学生的学号:");
String num = scan.next();
int Location = L.LocateElem_num(num);
Node node_current = L.GetElem(Location);
System.out.println(Location + "查找到学生信息如下:");
System.out.println("学号\t\t姓名\t\t成绩" );
System.out.println(node_current.getNum() + "\t"+ node_current.getName() + "\t\t" + node_current.getScore());
//暴力清屏逻辑
Clean(L);
}
//按姓名查找
public static void Location_name(LinkList L) {
System.out.print("请输入查找学生的姓名:");
String name = scan.next();
int Location = L.LocateElem_name(name);
Node node_current = L.GetElem(Location);
System.out.println("查找到学生信息如下:");
System.out.println("学号\t\t姓名\t\t成绩" );
System.out.println(node_current.getNum() + "\t"+ node_current.getName() + "\t\t" + node_current.getScore());
//暴力清屏逻辑
Clean(L);
}
//按成绩排序
public static void Sort_score(LinkList L) {
System.out.print("请选择排序方式(从高到低:1 从低到高:2):");
int flag = scan.nextInt();
if(flag == 1) {
System.out.println("将成绩从高到低排序");
L.SortList();
L.ReverseList();
}else {
System.out.println("将成绩从低到高排序");
L.SortList();
}
//暴力清屏逻辑
Clean(L);
}
//显示所有学生的信息
public static void Print_all(LinkList L) {
System.out.println("学号\t\t\t姓名\t\t成绩" );
L.ListTraverse();
//暴力清屏逻辑
Clean(L);
}
//退出
public static boolean Exite(boolean flag) {
System.out.println("您确定要退出吗?(Y/N)");
String exit = scan.next();
char exit_chr = exit.charAt(0);
if(exit_chr == 'Y') {
flag = false;
}
return flag;
}
//暴力清屏逻辑
public static void Clean(LinkList L) {
System.out.print("是否进行清屏操作(Y/Others):");
String flag2 = scan.next();
char flag_chr2 = flag2.charAt(0);
if(flag_chr2 == 'Y') {
L.Clean();
}
}
//String型数组前移一位
public static void Array_displace(int n, String[] array ) {
String temp1 = array[n];
array[n] = array[n+1];
array[n+1] = temp1;
}
//int型数组前移一位(这里运用到了方法的重载)
public static void Array_displace(int n, int[] array ) {
int temp1 = array[n];
array[n] = array[n+1];
array[n+1] = temp1;
}
}LinkList类:
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
226package model.bean;
public class LinkList {
//属性:
Node node_head;
//方法:
//长度:
public int Length() {
int length = 0;
Node node_current = node_head;
while(node_current != null) {
length ++;
node_current = node_current.next;
}
return length;
}
//用元素e返回第i个元素的值
public Node GetElem(int i) {
//判断i是否合法
if(i > Length() || i <= 0)
return null;
//将位置节点指向头节点并声明计数变变量来表示位置节点的位置
Node node_current = node_head;
int count = 1;
//令node_current的地址存储指定位置的节点的地址
while(count != i) {
node_current = node_current.next;//将node_current的地址改为下一个节点的地址
count ++;
}
//返回第i个节点的地址
return node_current;
}
//返回链表中第一个与元素e相同的元素的位置(num)
public int LocateElem_num(String e) {
//将位置节点指向头节点并声明计数变变量来表示位置节点的位置
Node node_current = node_head;
int count = 1;
while( count != Length() + 1) {
if(node_current.getNum().equals(e))
break;
node_current = node_current.next;
count ++;
}
if(node_current != null)
return count;
else
return 0;
}
//返回链表中第一个与元素e相同的元素的位置(name)
public int LocateElem_name(String e) {
//将位置节点指向头节点并声明计数变变量来表示位置节点的位置
Node node_current = node_head;
int count = 1;
while( count != Length() + 1) {
if(node_current.getName().equals(e))
break;
node_current = node_current.next;
count ++;
}
if(node_current != null)
return count;
else
return 0;
}
//插入
//添加新节点
public void add(String e1, String e2, int e3) {
if(node_head == null) {
Node node = new Node(e1, e2, e3);
node_head = node;
}
else {
//声明一个新节点
Node node_new = new Node(e1, e2, e3);
Node node_current = node_head;
//把node_current设置为最后一个节点
while(node_current.next != null) {
node_current = node_current.next;
}
//添加新节点
node_current.next = node_new;
node_new.next = null;
}
}
//在指定位置插入新节点
public void ListInsert(int i, String e1, String e2, int e3) {
//判断i是否超出链表长度
if(i > Length() + 1)
return;
//判断i是否是最后一个节点
if(i == Length() + 1) {
add(e1, e2, e3);
return;
}
//在链表首端插入节点
else if(i == 1) {
//新建节点并进行赋值
Node node_new =new Node(e1, e2, e3);
//将节点插入链表首端
node_new.next = node_head;
node_head = node_new;
}
//在链表中间插入节点
else{
//声明一个新节点并赋值
Node node_new =new Node(e1, e2, e3);
//将位置节点指向头节点并声明计数变变量来表示位置节点的位置
Node node_current = node_head;
int location = 1;
//把位置节点移到指定位置前一个的节点
while(location != i -1) {
//将位置节点后移一位
node_current = node_current.next;
location ++;
}
//将新的节点插入
node_new.next = node_current.next;
node_current.next = node_new;
}
}
//删除第i个元素
public boolean ListDelete(int i) {
//判断i是否超出链表长度
if(i > Length())
return false;
else {
if(i == 1) {
node_head = node_head.next;
}
else {
//把node_current移到i前一位的位置
//将位置节点指向头节点并声明计数变变量来表示位置节点的位置
Node node_current = node_head;
int location = 1;
//令位置节点指向指定节点的前一位
while(location != i -1) {
node_current = node_current.next;
location ++;
}
//删除第i个节点
node_current.next = node_current.next.next;
}
}
return true;
}
//链表从低到高排序
public void SortList() {
if(node_head == null || node_head == null)
return;
Node quiet = node_head;
Node sport = quiet.next;
int loc_quiet;
int loc_sport;
for(loc_quiet = 1; loc_quiet <= Length()-1; loc_quiet ++) {
sport = quiet.next;
for(loc_sport = loc_quiet+1; loc_sport <= Length(); loc_sport ++) {
if(quiet.getScore() > sport.getScore()) {
Link_change_num(quiet, sport);
Link_change_name(quiet, sport);
Link_change_score(quiet, sport);
}
sport = sport.next;
}
quiet = quiet.next;
}
}
//单链表的逆置
public void ReverseList() {
//收尾指针第一个节点
Node tail = node_head;
//工作指针pre p
Node pre = node_head;
Node p = pre.next;
while (null != p){
Node next = p.next;
p.next = pre;
pre = p;
p = next;
}
//更新头指针
node_head = pre;
//尾指针指针域置空,防止出现环
tail.next = null;
}
//遍历链表元素
public void ListTraverse() {
if(Length() != 0) {
//将位置节点指向头节点并声明计数变变量来表示位置节点的位置
Node node_current = node_head;
int count = 1;
while(count != (Length() + 1)) {
System.out.println(node_current.getNum() + "\t\t" + node_current.getName() +"\t\t" + node_current.getScore());
node_current = node_current.next;
count ++;
}
}
}
//num数值交换
private void Link_change_num(Node node1, Node node2) {
String temp1 = node1.getNum();
node1.setNum(node2.getNum());
node2.setNum(temp1);
}
//score数值交换
private void Link_change_name(Node node1, Node node2) {
String temp1 = node1.getName();
node1.setName(node2.getName());
node2.setName(temp1);
}
//score数值交换
private void Link_change_score(Node node1, Node node2) {
int temp1 = node1.getScore();
node1.setScore(node2.getScore());
node2.setScore(temp1);
}
//暴力清屏逻辑
public void Clean() {
for(int i = 0; i <= 30; i ++) {
System.out.println("");
}
}
}Node类:
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
46package model.bean;
/**
* @项目名称:Student_Information_System_Link
* @包名称:model.bean
* @类名称:Node
* @类描述:用于存储数据
* @创建人:青春玩命的年代
* @创建时间:2021年10月26日
* @版本:
*/
public class Node {
//属性
private String num;
private String name;
private int score;
Node next;
//构造器
public Node(){
}
public Node(String e1, String e2, int e3){
num = e1;
name = e2;
score = e3;
}
//方法:
public String getNum() {
return this.num;
}
public String getName() {
return this.name;
}
public int getScore() {
return this.score;
}
public void setNum(String num) {
this.num = num;
}
public void setName(String name) {
this.name = name;
}
public void setScore(int score) {
this.score = score;
}
}