随笔 - 54,  文章 - 2,  评论 - 8,  阅读 - 1566

#####学习心得#####

今日讲了昨天留的一道题,对我来说有一些难度,但是前一天一直在听人讲解,最终写完了,但是没有太多的优化,东西多了就分不清了,要多练一练。

#####心情#####

不跟别人比较,只跟自己比,只要比前一天更好了就算没白努力。

 

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
package shang;
 
import java.util.Scanner;
 
public class Ch01 {
 
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
        int [] nos = new int[2];
        String [] names = new String[2];
        // 初始的工号
        int no = 1001;
        // 要操作的数组的下标
        int i = 0;
        while(true){
            System.out.println("欢迎使用员工管理系统");
            System.out.println("请选择功能:1.添加员工  2.查询员工  3.修改员工  4.删除员工");
            String flag = sc.next();
            switch (flag) {
                case "1":
 
                    System.out.println("请输入员工姓名:");
                    String name = sc.next();
                /*接下来要做得事:
                    把员工的姓名和工号保存到对应的数组里
                    有问题?
                    如果第一次输入的员工信息,保存到0的位置
                    第二次输入的员工信息,保存到1的位置
                 */
//                nos[0] = no;
//                names[0] = name;
//                no++;
//                nos[1] = no;
//                names[1] = name;
                    nos[i] = no;
                    names[i] = name;
                    System.out.println("添加成功:员工的工号为:" + nos[i] + ",姓名:" + names[i]);
                    i++;
                    no++;
                    /*
                        分析:
                        怎么才能知道即将要越界?
                        1.数组的最后一个元素不为null,0
                        2.当i的值和数组的最大下标,在进入到添加功能就先判断数组的下标是否合法
                        3.在添加成功之后,i自增之后,在判断下一次的数组的下标是否合法
                     */
                    if(i >= nos.length){
                        // 数组要扩容
                        int [] newNos = new int[nos.length + 1];
                        String [] newNames = new String[names.length + 1];
                        for (int j = 0; j < nos.length; j++) {
                            // 数组中的数据的复制
                            newNos[j] = nos[j];
                            newNames[j] = names[j];
                        }
                        // 重新赋值
                        nos = newNos;
                        names = newNames;
                    }
                    break;
                case "2":
                    System.out.println("请选择功能:1、根据工号查询  2、查询所有");
                    String n = sc.next();
                    switch (n) {
                        case "1":
                            System.out.println("请输入要查询的员工号:");
                            int s = sc.nextInt();
//                            boolean b = false;
                            // 先要去工号的数组中找,如果工号存在,拿到工号对应的下标
                            // 我还要拿着这个下标去姓名的数组中找姓名
                            int index = -1;
                            for (int j = 0; j < nos.length; j++) {
                                if(nos[j] == s){
                                    // 找到了
//                                    System.out.println("工号:" + s + ",姓名:" + names[j]);
//                                    b = true;
                                    index = j;
                                    break;
                                }
//                                else {
//                                    System.out.println("工号:" + s + "不存在!");
//                                }
                            }
                            if(index!= -1) {
                                System.out.println("工号:" + s + ",姓名:" + names[index]);
                            }else {
                                System.out.println("工号:" + s + "不存在!");
                            }
                            break;
                        case "2":
                            for (int j = 0; j < nos.length; j++) {
                                if(nos[j] != 0){
                                    System.out.println("工号:" + nos[j] + ",姓名:" + names[j]);
                                }
                            }
                            break;
                        default:
                    }
 
                    break;
 
                case "3":
                    System.out.println("请输入要修改的工号:");
                    int x = sc.nextInt();
                    /*
                        修改实际上是两个操作:
                        修改之前要先根据工号查询,看看这个人在不在我们的数组里
                        如果在,就执行修改的操作
                        如果不在,则显示当前用户不存在
                     */
                    int index = -1;
                    for (int j = 0; j < nos.length; j++) {
                        if(nos[j] == x){
                            index = j;
                            break;
                        }
                    }
                    if(index!= -1) {
                        System.out.println("工号:" + x + ",姓名:" + names[index]);
                        System.out.println("请输入新的姓名:");
                        String newName = sc.next();
                        names[index] = newName;
                        System.out.println("修改成功!工号:" + x + ",姓名:" + names[index]);
                    }else {
                        System.out.println("工号:" + x + "不存在!");
                    }
                    break;
                case "4":
                    /*
                        分析需求:
                        删除nos = 0,names = null
                        nos=0 names=null
                        移位
                        1,2,3,4,5
                        3删除掉
                        1,2,0,4,5
                        1,2,4,5,0
 
                        思路:
                        1,2,3,4,5
                        1,2,0,4,5
                        新建一个数组
                        把我现在的数组中的数据重新放进新的数组,不包括0
                        int [] arr = {1,2,3,4,5}
                        int [] arr = {1,2,0,4,5}
 
                        int [] newArr = new int[arr.length];
                     */
                    /*
                        删除:输入工号,是不是也得先查询
                     */
                    System.out.println("请输入要查询的员工号:");
                    int s = sc.nextInt();
                    // 先要去工号的数组中找,如果工号存在,拿到工号对应的下标
                    // 我还要拿着这个下标去姓名的数组中找姓名
                    int y = -1;
                    for (int j = 0; j < nos.length; j++) {
                        if(nos[j] == s){
                            y = j;
                            break;
                        }
                    }
                    if(y!= -1) {
                        System.out.println("工号:" + s + ",姓名:" + names[y]);
                        // 找到了,就删除
                        nos[y] = 0;
                        names[y] = null;
 
                        int [] newNos = new int[nos.length - 1];
                        String [] newNames = new String[names.length - 1];
 
                        for (int j = 0; j < nos.length - 1; j++) {
                            if(nos[j] == 0){
                                newNos[j] = nos[j + 1];
                                nos[j + 1] = 0;
                            }else {
                                newNos[j] = nos[j];
                            }
                            if(names[j] == null){
                                newNames[j] = names[j + 1];
                                names[j + 1] = null;
                            }else {
                                newNames[j] = names[j];
                            }
                        }
                        nos = newNos;
                        names = newNames;
                        System.out.println("工号:" + s + "删除成功!");
                    }else {
                        System.out.println("工号:" + s + "不存在!");
                    }
                    break;
                default:
            }
        }
 
    }
}

 

  这个是员工信息管理系统,填入和删除稍微难一点,就是需要用到的东西多一点,终于明白扩容是怎么去扩容了,是随着加入而改变的,这样的思维要多练一练,真的有时候是毫无头绪啊,但是班级里的一些基础很好的同学也是很鼓励大家,说自己一开始学的时候也是很苦恼,不要放弃。

 


import java.util.Scanner;

public class Ch01 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
int [] nos = new int[2];
String [] names = new String[2];
// 初始的工号
int no = 1001;
// 要操作的数组的下标
int i = 0;
while(true){
System.out.println("欢迎使用员工管理系统");
System.out.println("请选择功能:1.添加员工 2.查询员工 3.修改员工 4.删除员工");
String flag = sc.next();
switch (flag) {
case "1":

System.out.println("请输入员工姓名:");
String name = sc.next();
/*接下来要做得事:
把员工的姓名和工号保存到对应的数组里
有问题?
如果第一次输入的员工信息,保存到0的位置
第二次输入的员工信息,保存到1的位置
*/
// nos[0] = no;
// names[0] = name;
// no++;
// nos[1] = no;
// names[1] = name;
nos[i] = no;
names[i] = name;
System.out.println("添加成功:员工的工号为:" + nos[i] + ",姓名:" + names[i]);
i++;
no++;
/*
分析:
怎么才能知道即将要越界?
1.数组的最后一个元素不为null0
2.i的值和数组的最大下标,在进入到添加功能就先判断数组的下标是否合法
3.在添加成功之后,i自增之后,在判断下一次的数组的下标是否合法
*/
if(i >= nos.length){
// 数组要扩容
int [] newNos = new int[nos.length + 1];
String [] newNames = new String[names.length + 1];
for (int j = 0; j < nos.length; j++) {
// 数组中的数据的复制
newNos[j] = nos[j];
newNames[j] = names[j];
}
// 重新赋值
nos = newNos;
names = newNames;
}
break;
case "2":
System.out.println("请选择功能:1、根据工号查询 2、查询所有");
String n = sc.next();
switch (n) {
case "1":
System.out.println("请输入要查询的员工号:");
int s = sc.nextInt();
// boolean b = false;
// 先要去工号的数组中找,如果工号存在,拿到工号对应的下标
// 我还要拿着这个下标去姓名的数组中找姓名
int index = -1;
for (int j = 0; j < nos.length; j++) {
if(nos[j] == s){
// 找到了
// System.out.println("工号:" + s + ",姓名:" + names[j]);
// b = true;
index = j;
break;
}
// else {
// System.out.println("工号:" + s + "不存在!");
// }
}
if(index!= -1) {
System.out.println("工号:" + s + ",姓名:" + names[index]);
}else {
System.out.println("工号:" + s + "不存在!");
}
break;
case "2":
for (int j = 0; j < nos.length; j++) {
if(nos[j] != 0){
System.out.println("工号:" + nos[j] + ",姓名:" + names[j]);
}
}
break;
default:
}

break;

case "3":
System.out.println("请输入要修改的工号:");
int x = sc.nextInt();
/*
修改实际上是两个操作:
修改之前要先根据工号查询,看看这个人在不在我们的数组里
如果在,就执行修改的操作
如果不在,则显示当前用户不存在
*/
int index = -1;
for (int j = 0; j < nos.length; j++) {
if(nos[j] == x){
index = j;
break;
}
}
if(index!= -1) {
System.out.println("工号:" + x + ",姓名:" + names[index]);
System.out.println("请输入新的姓名:");
String newName = sc.next();
names[index] = newName;
System.out.println("修改成功!工号:" + x + ",姓名:" + names[index]);
}else {
System.out.println("工号:" + x + "不存在!");
}
break;
case "4":
/*
分析需求:
删除nos = 0,names = null
nos=0 names=null
移位
1,2,3,4,5
3删除掉
1,2,0,4,5
1,2,4,5,0

思路:
1,2,3,4,5
1,2,0,4,5
新建一个数组
把我现在的数组中的数据重新放进新的数组,不包括0
int [] arr = {1,2,3,4,5}
int [] arr = {1,2,0,4,5}

int [] newArr = new int[arr.length];
*/
/*
删除:输入工号,是不是也得先查询
*/
System.out.println("请输入要查询的员工号:");
int s = sc.nextInt();
// 先要去工号的数组中找,如果工号存在,拿到工号对应的下标
// 我还要拿着这个下标去姓名的数组中找姓名
int y = -1;
for (int j = 0; j < nos.length; j++) {
if(nos[j] == s){
y = j;
break;
}
}
if(y!= -1) {
System.out.println("工号:" + s + ",姓名:" + names[y]);
// 找到了,就删除
nos[y] = 0;
names[y] = null;

int [] newNos = new int[nos.length - 1];
String [] newNames = new String[names.length - 1];

for (int j = 0; j < nos.length - 1; j++) {
if(nos[j] == 0){
newNos[j] = nos[j + 1];
nos[j + 1] = 0;
}else {
newNos[j] = nos[j];
}
if(names[j] == null){
newNames[j] = names[j + 1];
names[j + 1] = null;
}else {
newNames[j] = names[j];
}
}
nos = newNos;
names = newNames;
System.out.println("工号:" + s + "删除成功!");
}else {
System.out.println("工号:" + s + "不存在!");
}
break;
default:
}
}

}
}
posted on   骐琳  阅读(36)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
你点我就回上面去了ヾ(≧O≦)〃嗷~
点击右上角即可分享
微信分享提示