java数组简单逻辑代码

package cuteSnow;

public class HelloWorld {
    // 遍历数组里面的每个数字
    public static void print(int[] array){
        String tip = "数组中每个元素分别为:";
        for(int i = 0; i<array.length; i++) {
           int temNum = array[i];
           tip = tip + temNum +" "; 
        }
        System.out.print(tip);
    }
    // 判断给定数据是否为6的倍数
    public static void sixMultiple(int[] array) {
        String tip = "6的倍数有:";
        for(int i = 0; i<array.length; i++) {
            int temNum = array[i];
            if( temNum % 6 == 0) {
                tip = tip + temNum +" ";
            }
        }
        System.out.print(tip);
    }
    // 将数组第一个数字和最后一个数字对调
    public static void exchange(int[] array) {
        String tip = "数组第一个数字和最后一个数字对调之后的数组为:[";
        int tem = array[array.length - 1];//最后一个的值存起来
        array[array.length - 1] = array[0];
        array[0] = tem;
        for(int i = 0; i<array.length; i++) {
            int temNum = array[i];
            tip = tip + temNum;
            if(i < array.length-1) {
                tip = tip + ",";
            }
        }
        System.out.print(tip+"]");
    }
    // 将数组的元素倒序排列
    public static void desc(int[] array) {
        String tip = "数组的元素倒序排列为:[";
        for(int i = 0; i<array.length/2; i++) {
            int tem = array[i];
            array[i] = array[array.length - i - 1];
            array[array.length - 1 - i] = tem;
        }
        for(int i = 0; i<array.length; i++) {
            int temNum = array[i];
            tip = tip + temNum;
            if(i < array.length-1) {
                tip = tip + ",";
            }
        }
        System.out.print(tip+"]");
    }
    //求1000以内的质数,存放到数组中
    public static void prime() {
        String tip = "1000以内的质数组合成的数组为:[";
        int[] prime = new int[50]; // 最多存放50个
        int count = 0;
        for(int i = 2;i <= 1000;i++) {
            boolean isPrime = true;
            for(int k = 2;k < i;k++) {
                if(i % k == 0) {
                    isPrime = false;
                    break;
                }
            }
            if(isPrime) {
                prime[count] = i; 
                count++;
                if(count >= prime.length) {
                    break;
                }
            }
        }
        for(int i = 0; i < count; i++) {
            int temNum = prime[i];
            tip = tip + temNum;
            if(i < count-1) {
                tip = tip + ",";
            }
        }
        System.out.print(tip+"]");
    }
    
    // 主函数
    public static void main(String[] args) {
        // System.out.println("helloworld");
        int[] array = { 29, 90, 48, 92};
        // 调用
        print(array);
        System.out.print("
");
        sixMultiple(array);
        System.out.print("
");
        exchange(array);
        System.out.print("
");
        desc(array); // 由于数组时引用类型,所以此处是数组值受exchange(array)方法的影响值为[92,90,48,29]
        System.out.print("
");
        prime();
    }
// 上述运行结果
数组中每个元素分别为:29 90 48 92 
6的倍数有:90 48 
数组第一个数字和最后一个数字对调之后的数组为:[92,90,48,29]
数组的元素倒序排列为:[29,48,90,92]
1000以内的质数组合成的数组为:[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229]



}
原文地址:https://www.cnblogs.com/cuteCoderSnow/p/10135217.html