2022-07-16 长春大学 第三小组 赖哲栋
作者:互联网
目录
**知识重点:** 1、数组倒叙扩容 2、管理系统 3、数组指定位置插入 4、通过下标获取指定位置 5、倒叙输入数组数据
**知识点掌握:**了解
**个人心得:** 今天讲的是对前几天知识的应用和巩固,编程时出现了许多问题,所幸都解决了,锻炼了逻辑思维。每天都争取多收获一点 加油!!
**知识重点:** 1、数组倒叙扩容 2、管理系统 3、数组指定位置插入 4、通过下标获取指定位置 5、倒叙输入数组数据
**知识点掌握:**了解
**个人心得:** 今天讲的是对前几天知识的应用和巩固,编程时出现了许多问题,所幸都解决了,锻炼了逻辑思维。每天都争取多收获一点 加油!!
1. 员工管理系统
自己写的代码
import java.util.Scanner;
public class ManageSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("欢迎使用员工管理系统:");
int id = 1001;
String[] names = new String[10];
int[] nos = new int[10];
int i = 0;
int index = -1;
a:
for (; ; ) {
System.out.println("请选择功能: 1、添加员工 2、查询员工 3、修改员工信息 4、删除员工");
String flag = sc.next();
switch (flag) {
case "1":
System.out.println("请输入员工姓名:");
String num1 = sc.next();
names[i] = num1;
nos[i] = id;
System.out.println("添加员工成功," + "员工工号为:" + id + "员工姓名为:" + num1);
id++;
i++;
if (i == nos.length) {
int[] temp1 = new int[i+10];
for (int d = 0; d <nos.length ; d++) {
temp1[d] = nos[d];
}
nos = temp1;
String[] temp2 = new String[i+10];
for (int d = 0; d <names.length ; d++) {
temp2[d] = names[d];
}
names = temp2;
}
continue a;
case "2":
System.out.println("请输入要查询的工号:");
int num2 = sc.nextInt();
boolean b = true;
for (int j = 0; j < nos.length - 1; j++) {
if (num2 == nos[j]) {
System.out.println("查询员工成功," + "员工工号为:" + nos[j] + "员工姓名为:" + names[j]);
b = false;
break;
}
}
while (b) {
System.out.println("查无此人");
break;
}
break;
case "3":
System.out.println("请输入要修改的工号:");
int num3 = sc.nextInt();
for(int k = 0; k < nos.length - 1; k++){
if(num3 == nos[k]){
System.out.println("查询员工成功," + "员工工号为:" + nos[k] + "员工姓名为:" + names[k]);
System.out.println("请输入要修改的员工工号"+nos[k]+"的新名字:");
String num4 = sc.next();
names[k] = num4;
index = k;
break;
}
}
if(index == -1){
System.out.println("查无此人");
}
}
}
}
}
老师给的代码
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();
nos[i] = no;
names[i] = name;
System.out.println("添加成功:员工的工号为:" + nos[i] + ",姓名:" + names[i]);
i++;
no++;
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();
int index = -1;
for (int j = 0; j < nos.length; j++) {
if(nos[j] == s){
index = j;
break;
}
}
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":
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:
}
}
}
}
2. 通过下标获取指定位置的数字
import java.util.Scanner;
public class Ch02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[]{4, 58, 69, 14, 22, -1};
System.out.println("请输入一个下标:");
int index = sc.nextInt();
if (index < arr.length && index >= 0) {
System.out.println("对应位置的值是:" + arr[index]);
}else{
System.out.println("你输入的数据有误,无法获取");
}
}
}
3. 给数组倒叙输入数值
import java.util.Arrays;
import java.util.Scanner;
public class Ch03 {
public static void main(String[] args) {
int[] arr = new int[5];
Scanner sc = new Scanner(System.in);
for(int i = arr.length-1;i>=0;i--){
System.out.println("请输入第"+(i+1)+"的值");
int a = sc.nextInt();
arr[i] = a;
}
System.out.println(Arrays.toString(arr));
}
}
4. 数组倒叙扩容
import java.util.Arrays;
import java.util.Scanner;
public class Ch04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[2];
int i = 0;
a:
while (true) {
System.out.println("请输入数据:");
int num = sc.nextInt();
arr[arr.length - 1 -i] = num;
i++;
if (i >= arr.length) {
int[] newArr = new int[arr.length * 2];
for (int j = 0; j < arr.length; j++) {
newArr[newArr.length - 1 - j] = arr[arr.length - 1 - j];
}
arr = newArr;
}
System.out.println("是否要继续添加 1.是 2.否");
String flag = sc.next();
switch (flag) {
case "1":
continue;
case "2":
System.out.println("当前数组为" + Arrays.toString(arr));
break a;
}
}
}
}
5. 数组指定位置插入
import java.util.Arrays;
import java.util.Scanner;
public class Ch05 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[]{1, 2, 3, 4, 5};
int[] newArr = new int[arr.length * 2];
for (int j = 0; j < arr.length; j++) {
newArr[j] = arr[j];
}
arr = newArr;
main:
while (true) {
System.out.println("请输入要插入数据的下标:");
int index = sc.nextInt();
System.out.println("请输入要插入的数据:");
int num = sc.nextInt();
int temp = arr[index];
arr[index] = num;
for (int i = 1; i < arr.length - index; i++) {
arr[arr.length - i] = arr[arr.length - 1 - i];
}
arr[index + 1] = temp;
System.out.println("现在数组为:" + Arrays.toString(arr));
System.out.println("是否继续添加:1、是 2、否");
String flag = sc.next();
switch (flag) {
case "1":
continue;
case "2":
System.out.println("当前数组为:" + Arrays.toString(arr));
break main;
}
}
}
}
标签:arr,07,16,int,System,2022,println,nos,out 来源: https://www.cnblogs.com/laizhedong/p/16485355.html