【数组综合作业】

1.请将”我” “爱” “中”“国”存入数组,然后正着和反着输出

 public static void main(String[] args) {
        print();

    }

    public static void print() {
        //创建一个数组
        String[] arr = new String[]{"我", "爱", "中", "国"};
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
        System.out.println();
        for (int i = arr.length - 1; i < arr.length; i--) {
            if (i < 0) {
                break;
            }
            System.out.print(arr[i]);
        }
    }
运行结果:

2.定义一个长度为10的整型数组,循环输入10个整数。 然后将输入一个整数,查找此整数,找到 输出下标,没找到给出提示。  

 public static void main(String[] args) {
        int[] arr = new int[10];
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入10个数");
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
        }
        while (true) {
            System.out.println("请输入要查找在的数");
            int a = sc.nextInt();
            for (int i = 0; i < arr.length; i++) {
                if (a == arr[i]) {
                    index = i;
                    System.out.println("该数的下标为:" + index);
                    return;
                }
            }
            System.out.println("没有您需要查找的数字");
        }
    }
}

运行结果:

【数组综合作业】_第1张图片

 3.求一个3*3矩阵对角线元素之和,要求矩形里面的值为随机赋值

  public static void main(String[] args) {
        int[][] arr = new int[3][3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.println("请输入数组中arr[" + i + "][" + j + "]" + "的值:");
                arr[i][j] = sc.nextInt();
            }
        }
        System.out.println("您输入的二维数组元素是:");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (i == j && i + j != 2 || i + j == 2) {
                    sum += arr[i][j];
                }
            }
        }
        System.out.println("您输入的二维数组对角线元素之和是:" + sum);
        sc.close();
    }
}

运行结果:

【数组综合作业】_第2张图片

4.自定义一个字符串数组,提示用户输入5个字符串,通过循环语句实现将用户输入的字符串存放到数

组里,然后反向输出这五个字符串。

public static void main(String[] args) {
        int[] arr = new int[5];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入数组中arr[" + i + "]的值:");
            arr[i] = sc.nextInt();
        }
        System.out.println("您输入的数组元素是:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        System.out.println("反转后的元素:");
        for (int j = arr.length - 1; j < arr.length; j--) {
            if (j < 0) {
                break;
            }
            System.out.print(arr[j] + " ");
        }
    }

}

运行结果:

【数组综合作业】_第3张图片

5.打印扑克牌

提示:可定义如下的数组

String[] hs = {"♠","♥","♣","♦"};

String[] ds = {"A","2","3","4", "5","6","7","8","9","10","J","Q","K"};

代码实现,效果如图所示:

♠A ♠2 ♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠10 ♠J ♠Q ♠K 
♥A ♥2 ♥3 ♥4 ♥5 ♥6 ♥7 ♥8 ♥9 ♥10 ♥J ♥Q ♥K 
♣A ♣2 ♣3 ♣4 ♣5 ♣6 ♣7 ♣8 ♣9 ♣10 ♣J ♣Q ♣K 
♦A ♦2 ♦3 ♦4 ♦5 ♦6 ♦7 ♦8 ♦9 ♦10 ♦J ♦Q ♦K 

    public static void main(String[] args) {
        String[] hs = {"♠", "♥", "♣", "♦"};
        String[] ds = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        //遍历两个数组打印
            for (int i = 0; i < hs.length; i++) {
                for (int j = 0; j < ds.length; j++) {
                    //拼接字符串
                    System.out.print( hs[i] + ds[j]);
                }
                //换行输出
                System.out.println();
            }
        }
   

运行结果:

【数组综合作业】_第4张图片

 6.判断数组中的元素值是否对称.

效果展示:java
{1,2,3,4,3,2,1}是否对称:true
{1,2,3,4,5,6,7}是否对称:false

 public static void main(String[] args) {
        int[] arr = new int[7];
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入数组中arr的7个值:");
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
            System.out.print(arr[i] + " ");
        }
        boolean flag = true;
        for (int left = 0, right = arr.length - 1; left <= right; left++, right--) {
            if (arr[left] != arr[right]) {
                flag = false;
                System.out.println("是否对称:" + flag);
                break;
            } else {
                System.out.println("是否对称:" + flag);
                break;
            }
        }
    }
}

运行结果:

你可能感兴趣的:(java,开发语言)