java案例

public class Demo {
	//输出数组中所有的数据
		public void showData(int[] arr, int length) {
			for(int i = 0; i < length; i ++) {
				System.out.print(arr[i] + " ");
			}
			System.out.println();
		}
		
		//创建数据
		public int[] insertData() {
			//创建一个数组预先定义它可以存9个数据
			int[] a = new int[9];
			//监听键盘的操作,并接收用户输入
			Scanner sc = new Scanner(System.in);
			
			for(int i = 0; i < a.length - 1; i++) {
				System.out.println("请输入第" + (i + 1) + "数据");
				try {//当用户输入非数字的操作给出提示
					a[i] = sc.nextInt();
				} catch (InputMismatchException e) {
					System.out.println("输入的数据类型有误,不能是非数字");
					sc.next();
					i--;
				}
			}
			return a;
		}
		
		//插入数据
		public void insertAtArray(int[] a, int n, int k) {
			//首先将要插入位置处的数组元素都向后移动一位
			//要从最后一位数据开始移动,避免数据被覆盖
			for(int i = a.length - 1; i > k; i--) {
				//使前一位元素覆盖后一位元素
				a[i] = a[i - 1];
			} 
			//将要插入的数据赋值给指定位置处的数据
			a[k] = n;
		}
		
		//输出数组中能够被3整除的数据
		public void divThree(int[] a) {
			//用于保存能够被3整除的数据
			String str = "";
			//能够被3整除的元素的个数
			int count = 0;
			for(int n : a) {
				if(n % 3 == 0) {
					str = str + n + " ";
					count++;
				}
			}
			if(count == 0) {
				System.out.println("数组中没有能够被3整除的数据");
			} else {
				System.out.println("数组中能够被3整除的数据为:" + str);
			}
		}
		
		//提示信息
		public void notice() {
			System.out.println("***********************");
			System.out.println("   1 --插入数据");
			System.out.println("   2 --显示数据");
			System.out.println("   3 --在指定位置插入数据");
			System.out.println("   4 --查询能够被3整除的数据");
			System.out.println("   0 --退出");
			System.out.println("***********************");
		}
		
		public static void main(String[] args) {
			Demo de = new Demo();
			//监听键盘的操作,并接收用户输入
			Scanner sc = new Scanner(System.in);
			//先定义数组为空(并不是指空数组,而是指数组a指向的内存地址为空)
			int[] a = null;
			int input;
			//n 表示要插入数组的数据,k表示要插入的数组的位置
			int n = 0;
			int k = 0;
			while(true) {
				//循环提示
				de.notice();
				//并给出要求输入的提示
				System.out.println("请输入对应的数字进行操作:");
				//接收用户选择的代码
				try {
					input = sc.nextInt();
				} catch(InputMismatchException e) {
					System.out.println("输入的数据格式有误,不能非数字");
					sc.next();
					continue;
				}
				
				//根据用户的选择进行操作的判断
				//1.针对0单独进行处理,退出程序(退出循环)
				if(input == 0) {
					System.out.println("退出程序");
					break;
				}
				//2.针对1,2,3,4这四个选择的代码进行判断,并提供对应的服务
				//通过switch语句根据选择的代码进行操作的选择
				switch(input) {
					case 1: {//执行新增数据操作
						a = de.insertData();
						//显示用户新增的数据
						System.out.println("数组元素为:");
						de.showData(a, a.length - 1);
						break;
					}
					case 2: {//显示所有数据
						if(a != null) {
							System.out.println("数组元素为:");
							//如果数组的最后一个元素为0,可以简单地认为数组还没有插入数据,因此不显示最后一个元素
							if(a[a.length - 1] == 0) {
								de.showData(a, a.length - 1);
							} else {
								de.showData(a, a.length);
							}
						} else {
							System.out.println("还未在数组中插入数据");
						}
						break;
					}
					case 3: {//在指定位置插入数据
						if(a != null) {
							System.out.println("请输入要插入的数据:");
							try {
								n = sc.nextInt();
								System.out.println("请输入要插入数据的位置:");
								k = sc.nextInt();
							} catch(InputMismatchException e) {
								System.out.println("输入的数据格式有误,不能有非数字");
								sc.next();
								break;
							}
							//调用插入数据的方法
							de.insertAtArray(a, n, k);
							//插入数据后展示数据
							de.showData(a, a.length);
						} else {
							System.out.println("还未在数组中插入数据");
						}
						break;
					}
					case 4: {//查询能够被3整除的数据
						if (a != null) {
							de.divThree(a);
						} else {
							System.out.println("还未在数组中插入数据");
						}
						break;
					}
					default: {//防止用户输入1,2,3,4,0之外的数字
						System.out.println("请输入对应的数字进行操作!");
					}
				}
			}
			
		}

}

发布了19 篇原创文章 · 获赞 0 · 访问量 156

猜你喜欢

转载自blog.csdn.net/fanglingyu00/article/details/105430786