• Java数组移位和统计


    package com.imooc.method;
    
    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class DataManage {
        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(InputMismatchException e){
                    System.out.println("输入的数据格式有误,不能有非数字");
                    sc.next();
                    i--;
                }
            }
            return a;
        }
        /*
         * 显示数组中元素的内容
         * a:数组:
         * length :要显示的数组元素的个数:
         * */
        public void showData(int[] a,int length) {
            for(int i=0;i<length;i++) {
                System.out.print(a[i]+" ");
            }
            System.out.println();
        }
        /*从键盘接收一个数据,插入到数组的指定位置处:
         * 
         * */
        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) {
            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 自动生成的方法存根
            DataManage dm=new DataManage();
    //        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;
                }
            }
        }
    
    }

  • 相关阅读:
    Java基础
    HTML笔记
    Java简介
    win8 图标 汇总 希望做Windows 8 应用的朋友用得着
    amCharts 的完整使用及破解[我弄过的]
    找了几个地方:发现关于SQL 的基本使用sql日期精确推算
    使用powerdesigner生成Pdm
    windows 8 Metro 设置选中ListViewItem 的样式
    关于DataTable与IList和List泛型集合的相互转换在网上总结
    C++ RString (原生字符串)
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/12002197.html
Copyright © 2020-2023  润新知