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

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

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

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

 

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 2022-07-16 19:43  骐琳  阅读(35)  评论(1编辑  收藏  举报

你点我就回上面去了ヾ(≧O≦)〃嗷~