java数组移位_Java数组移位和统计

package com.imooc.method;

import java.util.InputMismatchException;

import java.util.Scanner;public classDataManage {public int[] insertData(){int[] a=new int[10];

Scanner sc=new Scanner(System.in);//少接收一个数据,为在指定位置处插入数据做准备;

for(int i=0;i

System.out.println("请输入第"+(i+1)+"个数据");try{

a[i]=sc.nextInt();

}catch(InputMismatchException e){

System.out.println("输入的数据格式有误,不能有非数字");

sc.next();

i--;

}

}returna;

}/** 显示数组中元素的内容

* a:数组:

* length :要显示的数组元素的个数:

**/

public void showData(int[] a,intlength) {for(int i=0;i

System.out.print(a[i]+" ");

}

System.out.println();

}/*从键盘接收一个数据,插入到数组的指定位置处:

*

**/

public void insertAtArray(int[] a,int n,intk) {for(int i=a.length-1;i>k;i--) {

a[i]=a[i-1];

}

a[k]=n;

}//获取能被3整除的元素:

public void divThree(int[] a) {

String str="";int count=0;for(intn:a) {if(n%3==0) {

str=str+n+" ";

count++;

}

}if(count==0) {

System.out.println("数组中没有能被3整除的元素");

}else{

System.out.println("数组中能被3整除的元素:"+str);

}

}public voidnotice() {

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 voidmain(String[] args) {//TODO 自动生成的方法存根

DataManage dm=newDataManage();//dm.notice();

Scanner sc=new Scanner(System.in);int input=0;int[] a=null;int n=0,k=0;//n表示要插入的数据,k是位置:

while(true) {

dm.notice();

System.out.println("请输入对应的数字进行操作");try{

input=sc.nextInt();

}catch(InputMismatchException e) {//TODO: handle exception

System.out.println("输入的数据格式有误,不能有非数字");

sc.next();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) {

dm.showData(a,a.length-1);

}

}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) {//TODO: handle exception

System.out.println("输入的数据格式有误,不能有非数字");

sc.next();break;

}

dm.insertAtArray(a, n, k);

dm.showData(a, a.length);

}else{

System.out.println("还未在数据中插入数据,请重新插入数据");

}break;case 4:if(a!=null) {

dm.divThree(a);

}else{

System.out.println("还未在数字中插入数据,请重新选择操作");

}break;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值