20200704数组移位与统计项目

需求分析
需求:定义一个类,对数组中的数据进行管理(增删改查)

package java_foundation;

import java.util.InputMismatchException;//异常类
import java.util.Scanner;//Scanner类
// Alt+/ 加载类

public class ArrayCase {
	final static int SETLENGTH = 10;//用于设置数组中初始元素数
	final static int MAX = 5;//用于设置可添加的元素上限
	static int length = SETLENGTH;//用于监控数组中的元素数
	public static void main(String[] args) {
		ArrayCase method = new ArrayCase();
		Scanner sc = new Scanner(System.in);
		int[] arr = null;
		int order;//用于接收指令序号
		while (true) {
			method.print();
			try {
				order = sc.nextInt();
				if (order < 0 || order >5) {
					System.out.println("输入的序号不存在,请重新输入。");
					continue;
				}
			}//检查是否有输入错误
			catch(InputMismatchException e) {
				System.out.println("输入的指令错误,请重新输入。");
				sc.next();//nextInt发生异常时程序对键盘的监听会中断。使用next方法重新连接键盘
				continue;
			}
			if (order == 0) {//放在switch内无法直接结束循环
				System.out.println("结束程序!");
				break;
			}
			switch (order) {
				case 1 : {
					length = SETLENGTH;
					arr = method.insertData();
					System.out.println("生成的数组为:");
					method.showData(arr, length);
					break;
				}
				case 2 : {
					if (arr == null){
						System.out.println("数组还未生成,请先生成数组");
					} else {
						System.out.println("查询的数组为:");
						method.showData(arr, length);
					}
					break;
				}
				case 3 : {
					if (arr == null){
						System.out.println("数组还未生成,请先生成数组");
					} else {
						int n, k;
						if (length == SETLENGTH + MAX){
							System.out.println("数组长度已经到达上限!");
						} else {
							try {//异常捕获,代码块中放置可能发生异常的代码
								System.out.println("请输入要插入的数据:");
								n = sc.nextInt();
								System.out.println("请输入要插入数据的索引:");
								k = sc.nextInt();
							}
							catch (InputMismatchException e) {//发生异常时执行
								System.out.println("输入的数据错误");
								sc.next();//nextInt发生异常时程序对键盘的监听会中断。使用next方法重新连接键盘
								break;
							}
							if (k < 0 || k > length) {
								System.out.println("输入的索引错误!");
								break;
							} else {
								method.insertAtArray(arr, n, k);
								System.out.println("插入数据后的数组为:");
								method.showData(arr, length);
							}
						}
					}
					break;
				}
				case 4 : {
					if (arr == null){
						System.out.println("数组还未生成,请先生成数组");
					} else {
						method.divThree(arr);
					}
					break;
				}
				case 5 : {
					if (arr == null){
						System.out.println("数组还未生成,请先生成数组");
					} else {
						int k;
						if (length == 0) {
							System.out.println("数组已被清空!");
						} else {
							try {
								System.out.println("请输入要删除数据的索引:");
								k = sc.nextInt();
							}
							catch (InputMismatchException e) {
								System.out.println("输入的数据错误");
								sc.next();//nextInt发生异常时程序对键盘的监听会中断。使用next方法重新连接键盘
								break;
							}
							if (k < 0 || k > length - 1) {
								System.out.println("输入的索引错误!");
								break;
							} else {
								method.deleteData(arr, k);
								System.out.println("删除数据后的数组为:");
								method.showData(arr, length);
							}
						}
					}
					break;
				}
					
			}
		}
	}

	
	/**
	 * 引导界面
	 */
	public void print() {
		System.out.println("请输入序号选择功能");
		System.out.println("1.输入数据生成数组");
		System.out.println("2.显示数组");
		System.out.println("3.在指定位置插入数据");
		System.out.println("4.查询能被3整除的元素");
		System.out.println("5.删除指定索引的元素");
		System.out.println("0.退出");
	}
	
	/**
	 * 向数组中插入数据
	 * @return 插入数据后的数组
	 */
	public int[] insertData() {
		int[] a = new int[length+MAX];//设置数组元素上限
		Scanner sc = new Scanner(System.in);//从外部进入程序的数据统称为输入型数据
		System.out.println("请输入" + SETLENGTH + "个数据");
		for (int i = 0; i < length; i++) {//预留MAX个空位用于插入数据
			System.out.println("请输入第" + (i+1) + "个数据");
			try {
				a[i] = sc.nextInt();
			}//检查是否有输入错误
			catch(InputMismatchException e) {
				System.out.println("输入数据错误");
				sc.next();//nextInt发生异常时程序对键盘的监听会中断。使用next方法重新连接键盘
				i--;
			}
		}
		return a;
	}
	
	/**
	 * 显示数组中前n个元素
	 * @param a int[] 显示的数组
	 * @param n int 显示的元素数
	 */
	public void showData(int[] a, int n) {
		for (int i = 0; i < n; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.print("\n");
	}
	
	/**
	 * 在指定位置插入数据,之后的数据向后移动
	 * @param a int[] 插入数据的数组
	 * @param n	int 插入的数据
	 * @param k int 插入数据的索引
	 */
	public void insertAtArray(int[] a, int n, int k) {
		for (int i = length; i > k; i--) {
			a[i] = a[i - 1];
		}
		a[k] = n;
		length++;
	}
	
	/**
	 * 查询能被3整除的数组元素
	 * @param a int[] 要查询的数组
	 */
	public void divThree(int[] a) {
		boolean flag = false;//用于判断是否存在能被3整除的元素
		for (int i = 0; i < length; i++) {
			if (a[i] % 3 == 0) {
				flag = true;//存在能被3整除的元素时改变值
				System.out.print(a[i] + " ");
			}
		}
		if (flag == false) {//遍历数组后不存在复合条件的元素时
			System.out.print("不存在能被3整除的数组元素");
		}
		System.out.print("\n");
	}
	
	/**
	 * 删除数组中指定索引位置的数据
	 * @param a int[] 要操作的数组
	 * @param k int 要删除元素的索引
	 */
	public void deleteData(int[] a, int k) {
		for (int i = k; i < length - 1; i++) {
			a[i] = a[i+1];
		}
		length--;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值