实现效果图
系统初始化
系统主要功能
项目源代码
package project0704
;
import java
.util
.InputMismatchException
;
import java
.util
.Scanner
;
public class DataManger {
public int[] insertData() {
int[] arr
= new int[10];
@SuppressWarnings("resource")
Scanner sc
= new Scanner(System
.in
);
for (int i
= 0; i
< arr
.length
- 1; i
++) {
System
.out
.println("请输入第" + (i
+ 1) + "个数据");
try {
arr
[i
] = sc
.nextInt();
} catch(InputMismatchException e
) {
System
.out
.println("提示:输入的数据类型有误!必须是整数!");
sc
.next();
i
--;
}
}
return arr
;
}
public void showData(int[] arr
, int length
) {
for (int i
= 0; i
< length
; i
++) {
System
.out
.print(arr
[i
] + " ");
}
System
.out
.println();
}
public void insertAtArray(int[] arr
, int n
, int k
) {
for (int i
= arr
.length
- 1; i
> k
- 1; i
--) {
arr
[i
] = arr
[i
- 1];
}
arr
[k
- 1] = n
;
}
public void divThree(int[] arr
, int length
) {
String str
= "";
for (int n
= 0; n
< length
; n
++) {
if (arr
[n
] % 3 == 0) {
str
= str
+ arr
[n
] + " ";
}
}
if (str
== "") {
System
.out
.println("数组中没有能被3整除的元素!");
} else {
System
.out
.println("数组中能被3整除的元素为:" + str
);
}
}
public void notice() {
printStar();
System
.out
.println(" 1--录入数据");
System
.out
.println(" 2--显示所有数据");
System
.out
.println(" 3--在指定位置处插入数据");
System
.out
.println(" 4--查询能被3整除的数据");
System
.out
.println(" 0--退出");
printStar();
}
public void printStar() {
System
.out
.println("********************************************");
}
public static void main(String
[] args
) {
DataManger dm
= new DataManger();
@SuppressWarnings("resource")
Scanner sc
= new Scanner(System
.in
);
int input
;
int[] a
= null
;
int n
= 0, k
= 0;
boolean flag
= false;
while (true) {
dm
.notice();
System
.out
.println("请输入对应的数字进行操作:");
try{
input
= sc
.nextInt();
} catch(InputMismatchException e
){
dm
.printStar();
System
.out
.println("提示:输入的数据格式有误!必须是整数!");
sc
.next();
continue;
}
if (input
== 0) {
System
.out
.println("退出程序!");
break;
}
switch (input
) {
case 1: {
a
= dm
.insertData();
dm
.printStar();
System
.out
.println("数组元素为:");
dm
.showData(a
, a
.length
-1);
flag
= false;
break;
}
case 2: {
if (a
== null
) {
dm
.printStar();
System
.out
.println("提示:还未在数组中录入数据!请重新选择操作!");
} else {
dm
.printStar();
System
.out
.println("数组元素为:");
if (flag
== false) {
dm
.showData(a
, a
.length
- 1);
} else {
dm
.showData(a
, a
.length
);
}
}
break;
}
case 3: {
if (a
== null
) {
dm
.printStar();
System
.out
.println("提示:还未在数组中录入数据!请重新选择操作!");
} else {
System
.out
.println("请输入要插入的数据:");
while(true) {
try {
n
= sc
.nextInt();
break;
} catch(InputMismatchException e
) {
System
.out
.println("提示:输入的数据格式有误!必须是整数!");
System
.out
.println("请重新输入要插入的数据:");
sc
.next();
}
}
System
.out
.println("请输入要插入数据的位置:");
while(true) {
try {
k
= sc
.nextInt();
while(true) {
if (k
< 1 || k
> 10) {
System
.out
.println("提示:插入位置越界!请输入1-10内的整数!");
System
.out
.println("请重新输入插入的位置:");
k
= sc
.nextInt();
} else {
break;
}
}
break;
} catch(InputMismatchException e
) {
System
.out
.println("提示:输入的插入位置有误!必须是整数!");
System
.out
.println("请重新输入插入的位置:");
sc
.next();
}
}
dm
.printStar();
System
.out
.println("提示:插入成功!");
flag
= true;
dm
.insertAtArray(a
, n
, k
);
dm
.showData(a
, a
.length
);
}
break;
}
case 4: {
if (a
== null
) {
dm
.printStar();
System
.out
.println("提示:还未在数组中录入数据!请重新选择操作!");
} else {
dm
.printStar();
if (flag
== false) {
dm
.divThree(a
, 9);
} else {
dm
.divThree(a
, 10);
}
}
break;
}
default: {
dm
.printStar();
System
.out
.println("提示:输入无效!");
}
}
}
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-54511.html