07-30 数组

一维数组定义

        int[] array1=new int[10];
        Scanner scanner=new Scanner(System.in);
        for (int i = 0; i < array1.length; i++) {
            array1[i]=scanner.nextInt();
        }
        for (int j = 0; j < array1.length; j++) {
            System.out.println(array1[j]);
        }
        int[] array2={1,2,3,4,5,6,7};
        for (int j = 0; j < 7; j++) {
            System.out.println(array2[j]);
        }

求平均值

        double[] scores=new double[5];
        Scanner scanner=new Scanner(System.in);
        double sum=0;
        for (int i = 0; i < scores.length; i++) {
            System.out.println("请输入数据:");
            scores[i]=scanner.nextDouble();
            sum=sum+scores[i];
        }
        System.out.println("平均分是"+sum/5);

冒泡排序

       int[] array={52,75,73,42,36,92};
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-i-1; j++) {
                int temp=0;
                if(array[j]>array[j+1]){
                    temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
        }
        //Arrays.sort(array);(排序方法)
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }

数组截取

int[] array={52,75,73,42,36,92};
int[] array1=Arrays.copyOfRange(array, 1,4);
for (int i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);
        }

数组复制

int[] array={52,75,73,42,36,92};
int[] array2=new int[10];
System.arraycopy(array, 1, array2, 1, 4);
for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i]);
}

二维数组定义

        int[][] array=new int[3][];
        array[0]=new int[]{1,2,3,4,5};
        System.out.println(array[0][1]);

        int[][] array1=new int [2][3];
        array1[1]=new int[]{1,2,3,4,5}; 
        System.out.println(array1[1][4]);

        int[][] array2={{1,2,3},{4,5,6},{7,8,9}};
        System.out.println(array2[1][2]);

判断邮箱地址

        System.out.println("请输入邮箱地址:");
        Scanner scanner=new Scanner(System.in);
        String email=scanner.next();

        if(email.contains("@")){
            int index=email.indexOf("@");
            String start=email.substring(0, index);
            boolean isLetterOrNum=true;
            for (int i = 0; i < start.length(); i++) {
                char ch=start.charAt(i);
                if(!Character.isLetterOrDigit(ch)){
                    break;
                }
            }
            if(isLetterOrNum==false){
                System.out.println("起始不是字母或数字!");
            }


            if(email.contains(".")){
                int index1=email.lastIndexOf(".");
                String center=email.substring(index+1, index1);
                boolean isNumOrLetter=true;
                for (int i = 0; i < center.length(); i++) {
                    char ch1=center.charAt(i);
                    if(!Character.isDigit(ch1)){
                        break;
                    }
                }
                if(isNumOrLetter==false){
                    System.out.println("中间不是数字!");
                }

            }else{
                System.out.println("缺少.符号!");
            }


            boolean end=false;
            if(email.endsWith(".com")||email.endsWith(".cn")){
                end=true;
                System.out.println("邮箱格式正确!");
            }else{
                System.out.println("结尾格式格式错误!");
            }


        }else{
            System.out.println("缺少@符号!");
        }

字符串切换大小写

        String str="asdQWQzxc";
        String s="";
        for (int i = 0; i < str.length(); i++) {
            char c=str.charAt(i);
            boolean islower=Character.isLowerCase(c);
            if(islower){
                c=Character.toUpperCase(c);
            }else{
                c=Character.toLowerCase(c);
            }
            s=s+c;
        }
        System.out.println(s);

随机产生10个两位数,冒泡排序,从小到大输出

        Random ran=new Random();
        int[] array=new int[10];
        for (int i = 0; i < array.length; i++) {
            array[i]=ran.nextInt(90)+10;
            System.out.print(array[i]+" ");
        }
        System.out.println();
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));

输入一个四维数组,求正,副对角线上数的乘积

        int[][] array=new int[4][4];
        Random ran=new Random();
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                array[i][j]=ran.nextInt(10);
                System.out.print(array[i][j]+"\t");
            }
            System.out.println();
        }

        int temp1=1;
        int temp2=1;
        for (int i = 0; i < array.length; i++) {
            temp1=temp1*array[i][i];  //array[i][i]可以直接取值 
            temp2=temp2*array[i][4-i-1];
        }
        System.out.println("主对角线乘机为:"+temp1);
        System.out.println("副对角线乘机为:"+temp2);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值