Java array exercises (after typing these questions, go to the next chapter) [03]

1, Practice ten questions every day to find your hand

1. Judgment sentence exercise: https://blog.csdn.net/m0_55586329/article/details/122441587

2. Circular sentence exercise: https://blog.csdn.net/m0_55586329/article/details/122451061

3. Theoretical knowledge of judgment sentence: https://blog.csdn.net/m0_55586329/article/details/116331989

4. Array theory and practical knowledge (highly recommended): https://blog.csdn.net/m0_55586329/article/details/116840485

1. The array {1, 3, 6, 7, 2, 5, 9, 0} is known to output the maximum and minimum numbers of the array.

public class Demo01 {
    public static void main(String[] args) {
        int[] arrays = {1, 3, 6, 7, 2, 5, 9, 0};
        // Method 1: traverse the array
        // First define the maximum value as the first bit, and then compare
        int max = arrays[0];
        int min = arrays[0];
        for (int i = 0; i < arrays.length; i++) {
            if (max < arrays[i]) {
                max = arrays[i];
            }
            if (min > arrays[i]) {
                min = arrays[i];
            }
        }
        System.out.println("Maximum Max=" + max + " minimum value Min=" + min);
        // Method 2: use the encapsulated method
        System.out.println("Max=" + Arrays.stream(arrays).max().getAsInt() + "  Min=" + Arrays.stream(arrays).min().getAsInt());
    }
}

2. If the array {1, 3, 6, 7, 2, 5, 9, 0} is known, how to sort the array from small to large? Finally print it out.

public class Demo02 {
    public static void main(String[] args) {
        int[] arrays = {1, 3, 6, 7, 2, 5, 9, 0};
        // Method 1: Select Sorting
        for (int i = 0; i < arrays.length; i++) {
            for (int j = i + 1; j < arrays.length; j++) {
                if (arrays[i] >= arrays[j]) {
                    int temp = arrays[i];
                    arrays[i] = arrays[j];
                    arrays[j] = temp;
                }
            }
        }
        System.out.println("Method 1: " + Arrays.toString(arrays));
        // Method 2: a simple line of code [note]: it will change the array. Use it with caution
        Arrays.sort(arrays);
        System.out.println(Arrays.toString(arrays));
    }
}

3. Known array: {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, write a program to guess the number: enter a number and output whether the number exists in the array?

public class Demo03 {
    public static void main(String[] args) {
        int[] arrays = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        for (int i = 0; i < arrays.length; i++) {
            if (arrays[i] == input) {
                System.out.println("true");
            }
        }
    }
}

4. Create a two-dimensional integer array with 3 rows and 3 columns. The first column has values of 1,2,3, the second column has values of 3,2,1, and the third column has values of 0,0,0. Loop out each element of the array.

public class Demo04 {
    public static void main(String[] args) {
        int[][] arrays = new int[][]{{1, 2, 3}, {3, 2, 1}, {0, 0, 0}};
        for (int i = 0; i < arrays.length; i++) {
            for (int j = 0; j < arrays[i].length; j++) {
                System.out.print(arrays[i][j] + " ");
            }
        }
    }
}

5. Create an array of integers with a length of 10, assign values to this array using the console scanner, and finally calculate the sum and average of all elements.

public class Demo05 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] arrays = new int[10];
        int sum = 0;
        for (int i = 1; i <= arrays.length; i++) {
            System.out.print("Please enter page" + i + "Integer:");
            arrays[i - 1] = scanner.nextInt();
            sum += arrays[i - 1];
        }
        System.out.println("Sum of elements:" + sum + " Average:" + sum / arrays.length);
    }
}

6. The number of equipment that game characters can wear is 7. One initial character equipment is {null, null, "memory helmet", "staff of judgment", null, "," ordinary straw sandals "}, where null indicates that there is no equipment worn at this position. Now it is required to add 3 pieces of equipment to the game characters. The equipment name is input by the console and can only be equipped in the position where it is not worn. Finally, output all packages.

public class Demo06 {
    public static void main(String[] args) {
        String[] arrays = new String[]{null, null, "Memory helmet", "Staff of judgment", null, "", "Ordinary straw sandals"};
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < arrays.length; i++) {
            if (arrays[i] == null) {
                System.out.print("Please equip yourself:");
                arrays[i] = scanner.next();
            }
        }
        System.out.println(Arrays.toString(arrays));
    }
}

7. Define an integer array with a length of 15. The value of each element is a random number. Finally, print out each element of the array

public class Demo07 {
    public static void main(String[] args) {
        Random random = new Random();
        int[] arrays = new int[15];
        for (int i = 0; i < arrays.length; i++) {
            arrays[i] = random.nextInt();
        }
        System.out.println(Arrays.toString(arrays));
    }
}

8. Input a number from the console to represent the number of students in a class, then create a string array representing the names of all students in the class, use the console to input student names and assign them to each element of the array, and finally output all student names.

public class Demo08 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please enter the number of students in the class:");
        int num = scanner.nextInt();
        String[] stuArrays = new String[num];
        for (int i = 1; i <= stuArrays.length; i++) {
            System.out.print("Please enter" + i + "Name of student:");
            stuArrays[i - 1] = scanner.next();
        }
        System.out.println(Arrays.toString(stuArrays));
    }
}

9. Define an array with a length of 5 as {1,2,3,4,5}, and another array with a value of {10,20,30,40,50}. Use the for loop to merge and splice into a new array (the length is the sum of the two array lengths)

public class Demo09 {
    public static void main(String[] args) {
        // The understanding of array properties is investigated. Once the length of the array is defined, it cannot be changed. Therefore, it is necessary to create a new array
        int[] arrays01 = {1, 2, 3, 4, 5};
        int[] arrays02 = {10, 20, 30, 40, 50};
        int[] newArrays = new int[arrays01.length + arrays02.length];
        for (int i = 0; i < newArrays.length; i++) {
            if (i >= arrays01.length) {
                newArrays[i] = arrays02[i - arrays01.length];
            } else {
                newArrays[i] = arrays01[i];
            }
        }
        System.out.println(Arrays.toString(newArrays));
    }
}

10. Defines an array of strings with a length of 5. Cycle through 5 names. Then enter a user's name, check whether there is this person's name in the array, output the subscript, and then modify the name to a new name, which is input by the console, and finally print all names.

public class Demo10 {
    public static void main(String[] args) {
        String[] arrays = new String[5];
        Scanner scanner = new Scanner(System.in);
        int index = 0;
        for (int i = 0; i < arrays.length; i++) {
            System.out.print("Please enter" + (i + 1) + "Name of user:");
            arrays[i] = scanner.next();
        }
        // Check whether the array has the name
        System.out.print("Please enter the name you want to find:");
        String name = scanner.next();
        for (int i = 0; i < arrays.length; i++) {
            if (name.equals(arrays[i])) {
                index = i;
                System.out.print("Please enter a new name:");
                arrays[i] = scanner.next();
            }
        }
        System.out.println(Arrays.toString(arrays) + "The subscript for this user is:" + index);
    }
}

Like friends can point a praise to support it!!!

Keywords: Java

Added by azylka on Thu, 13 Jan 2022 14:58:40 +0200