1. 程式人生 > 其它 >JAVA基礎語法05 陣列移位與統計

JAVA基礎語法05 陣列移位與統計

技術標籤:JAVA工程師java程式語言

JAVA基礎語法05

筆記

綜合案例:陣列移位與統計

  • 案例需求
    • 顯示資料,預先設定好10個數據。
    • 插入資料
    • 在指定位置插入
    • 可以被3整除
package com.demo01;

import java.util.Scanner;

/**
 * 從鍵盤接收資料儲存到陣列中,並對陣列進行管理
 * 
 * @author zzh
 *
 */
public class demo3 {

	/**
	 * 插入資料
	 * 
	 * @return a[]
	 */
	public int[] inSertData() {
		int[] a = new int[10];
		Scanner sc = new Scanner(System.in);
		// 由於學習階段留一位作為插入
		for (int i = 0; i < a.length - 1; i++) {
			System.out.print("請插入" + (i + 1) + "個數據:");
			// 輸入非數字的異常處理,使用在這邊捕捉異常
			try {
				a[i] = sc.nextInt();
			} catch (java.util.InputMismatchException e) {
				System.out.println("輸入的資料的格式有誤,不能有非數字!");
				// 原來接收第一次非數字的型別
				sc.next();
				i--; // 重新輸入剛才資料
			}
		}
		return a;
	}

	/**
	 * 顯示陣列中的所有元素
	 * 
	 * @param a:陣列
	 * @param length:顯示元素個數
	 */
	// 顯示資料
	public void showData(int[] a, int length) {
		for (int i = 0; i < length; i++) {
			System.out.print(" " + a[i]);
		}
		System.out.println();
	}
	/**
	 * 在指定位置插入資料
	 * 
	 * @param a:陣列
	 * @param b:資料
	 * @param c:位置
	 */
	public void inSertAtArray(int[] a, int b, int c) {
		// 從最後資料開始移動防止覆蓋
		for (int i = a.length - 1; i > c - 1; i--) {
			a[i] = a[i - 1];
		}
		a[c - 1] = b;
	}
	/**
	 * 輸出陣列中能被3整除
	 * 
	 * @param a
	 */
	public void divThree(int[] a,int length) {
		String str = "";
		int count = 0;
		for(int i=0;i<length;i++) {
			if(a[i]%3==0) {
				str+=a[i]+" ";
				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) {

		demo3 dm = new demo3();
		Scanner sc = new Scanner(System.in);
		int input;
		int[] a = null; // 初始化
		int b, c,d=0;
		while (true) {
			dm.notice();
			System.out.print("請輸入對應的資料:");
			try {
				input = sc.nextInt();
			} catch (java.util.InputMismatchException e) {
				System.out.println("輸入的資料的格式有誤,不能有非數字!");
				sc.next();
				continue;
			}
			if (input == 0) {
				System.out.println("退出程式!");
				break;
			}
			switch (input) {
			case 1:
				if(d==0) {
					// 插入資料
					a = dm.inSertData();
					System.out.println("陣列元素:");
					// 顯示 資料
					dm.showData(a, a.length - 1);
					// d 用於標記最後一位元素是否插入
					d++;
				}else {
					System.out.println("元素已經插入!");
				}
				break;
			case 2:
				if (a != null) {
					System.out.println("陣列元素:");
					// 檢視最後一個是否插入資料
					if (a[a.length - 1] == 0) {
						dm.showData(a, a.length - 1);
					} else {
						dm.showData(a, a.length);
					}
				} else {
					System.out.println("請先插入資料!");
				}
				break;
			case 3:
				if (a != null) {
					System.out.println("請輸入插入資料:");
					try {
						b = sc.nextInt();
						System.out.println("請輸入插入位置:");
						c = sc.nextInt();
					} catch (java.util.InputMismatchException e) {
						System.out.println("輸入的資料的格式有誤,不能有非數字!");
						sc.next();
						break;
					}
					dm.inSertAtArray(a, b, c);
					dm.showData(a, a.length);
					d--;
				} else {
					System.out.println("請先插入資料!");
				}
				break;
			case 4:
				if (a != null) {
					if(d==1) {
						dm.divThree(a, a.length-1);
					}else {
						dm.divThree(a, a.length);
					}
				} else {
					System.out.println("請先插入資料!");
				}
				break;
			}
		}
	}

}

結果圖