案例 展示:
代碼:
package com.imooc.datamanage;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* 從鍵盤接受整型數據存放到數組中, 並對數組中的數據進行管理
* @author Administrator
*
*/
public class DataManage {
/**
* 從鍵盤接收數據
* @return 接收數據的數據
*/
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.println("請輸入第"+(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();
}
/**
* 從鍵盤接收一個數據,插入到數組的指定位置處
* 比如數組是目前是10位(9位有參1位空參) 要想插入數據的位置
* 後幾位統一往後位移一位騰出當前空位 防止數據覆蓋
* 位移邏輯就是從最末位開始向右位移 直到要插入的位置數據位移完空出賦值即可
*
* @param a 要插入數據的數組
* @param n 要插入的數據
* @param k 要插入的位置,從0開始
*/
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整除的元素
* @param a
*/
public void divThree(int[] a) {
String str = "";
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) {
// TODO Auto-generated method stub
DataManage dm = new DataManage();
Scanner sc = new Scanner(System.in);
int input = 0;
int n=0,k=0; //n表示插入的數據,k表示插入的位置(從0開始)
int[] a = null;
while(true) {
dm.notice();
System.out.println("請輸入對應的數字進行操作:");
try {
input = sc.nextInt();
}catch(InputMismatchException e) {
System.out.println("輸入的數據格式有誤,不能有非數字!");
sc.next();
//continue 語句退出當前循環 但是進行下一次循環
//這面註釋如果不用continue input默認值就是0 就直接被執行到
//if(input==0)語句 直接退出該程序bug
continue;
}
if(input==0) {
System.out.println("退出程序!");
break;
}
switch(input) {
case 1: //插入數據
a = dm.insertData();
System.out.println("數組元素爲:");
dm.showData(a, a.length-1);
break;
case 2: //顯示數據
if(a!= null) {
System.out.println("輸入元素爲:");
if(a[a.length-1]==0) {
//如果數組的最後一個元素爲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 {
n=sc.nextInt();
System.out.println("請輸入要插入數據的位置:");
k=sc.nextInt();
}catch(InputMismatchException e){
System.out.println("輸入的數據格式有誤,不能是非數字!");
sc.next();
break;
}
dm.insertAtArray(a, n, k);
dm.showData(a,a.length);
}else {
System.out.println("還未在數組中插入數據,請重新選擇操作");
}
break;
case 4: //查詢數組中能被3整除的元素
if(a!=null) {
dm.divThree(a);
}else {
System.out.println("還未在數組中插入數據,請重新選擇操作");
}
break;
}
}
}
}