Random and array enhanced training

Random and array enhanced training
1. Requirements: Define a one-dimensional array with a length of 5, assign a value to each element. (The value of each element in the array is required to be a random number of 20-80) Traverse the array and print the value of each element.
import java.util.Random;

public class demo04 {<!-- -->
    public static void main(String[] args) {<!-- -->
        Random r = new Random();
        int[] arr = new int[5];
        for (int i = 0; i < arr. length; i ++ ) {<!-- -->
            int a = r.nextInt(61) + 20;
            arr[i] = a;
            System.out.println("The output random number is: " + arr[i]);
        }
    }
}
2. Requirements: Enter an integer as the length of the array, then enter the data again and fill the array, find the minimum value from the array, and print the minimum value on the console
import java.util.Random;
import java.util.Scanner;

public class demo05 {<!-- -->
    public static void main(String[] args) {<!-- -->
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the array length:");
        int a = sc. nextInt();
        int[] arr = new int[a];
        System.out.println("Please enter the array element:");
        for (int i = 0; i < arr. length; i ++ ) {<!-- -->
            arr[i] = sc. nextInt();
        }
        int min = arr[0];
        for (int i = 0; i < arr. length; i ++ ) {<!-- -->
            if (min > arr[i]) {<!-- -->
                min = arr[i];
            }
        }
        System.out.println("The minimum value is:" + min);
    }
}
3. Demand: the turnover of a supermarket in the four quarters of last year (unit ten thousand yuan)
  • They are: {11,22,33}, {44,55,66}, {77,88,99}, {11,22,33}
  • Please use the two-dimensional array technology you have learned to help the supermarket count the total turnover of each quarter and output it on the console
public class demo06 {<!-- -->
    public static void main(String[] args) {<!-- -->
        int[][] arr = {<!-- -->{<!-- -->11, 22, 33}, {<!-- -->44, 55, 66}, {<!-- -->77, 88, 99}, {<!-- -->11, 22, 33}};
        for (int i = 0; i < arr.length; i ++ ) {<!-- -->// Traverse the one-dimensional array of the two-dimensional array bit by bit
            int sum = 0;//Define a variable to find the sum of one-dimensional array
            for (int j = 0; j < arr[i].length; j ++ ) {<!-- -->// Traverse the elements of the one-dimensional array bit by bit
                sum + = arr[i][j];//Find the sum of one-dimensional array
            }
            System.out.println("th " + (i + 1) + "the total turnover of the quarter is" + sum);
        }
    }
}
operation result:
The total turnover in the first quarter was 66
The total turnover in the second quarter is 165
The total turnover in the third quarter is 264
4th quarter total turnover was 66

4, for loop exercise

Find the sum of the numbers between 1 and 100 that are both multiples of 3 and multiples of 5.

public class demo07 {
    public static void main(String[] args) {
        int num = 0;
        for (int i = 1; i < 101; i ++ ) {
            if (i % 3 == 0 & amp; & amp; i % 5 == 0) {
                num + = i;
            }
        }
        System.out.println("The sum of the numbers between 1 and 100 that are both multiples of 3 and multiples of 5 is: " + num);
    }
}
operation result:
The sum of the numbers between 1 and 100 that are both multiples of 3 and multiples of 5 is: 315

5. Enter a three-digit number greater than 100 from the keyboard, and find the sum of the numbers between 100 and the number that meet the following requirements:

? 1. The unit digit of the number is not 7;

2. The tens digit of the number is not 5;

3. The hundreds digit of the number is not 3;

import java.util.Scanner;

public class demo08 {<!-- -->
    public static void main(String[] args) {<!-- -->
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a three-digit number greater than 100:");
        int a = sc. nextInt();
        int sum = 0;
        for (int i = 101; i <= a; i ++ ) {<!-- -->
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100 % 10;
            if (ge != 7 & amp; & amp; shi != 5 & amp; & amp; bai != 3) {<!-- -->
                sum + = i;
            }
        }
        System.out.println(sum);
    }
}
operation result:
Please enter a three-digit number greater than 100:
103
306

5. Please write the program and print the menu according to the following requirements:

? 1. Enter a number from 1 to 5 from the keyboard;

? 2. When the number is 1, print the menu “New”;

? When the number is 2, print the menu “Open File”;

? When the number is 3, print the menu “Save”;

? Print menu “Refresh” when the number is 4;

? When the number is 5, print the menu “Exit” and exit the program;

? This line of code can terminate the java program: System.exit(0);

import java.util.Scanner;

public class demo09 {<!-- -->
    public static void main(String[] args) {<!-- -->
        Scanner sc = new Scanner(System.in);
        System.out.println("1, create 2, open file 3, save 4, refresh 5, exit");
        for (int i = 1; i <= 5; i ++ ) {<!-- -->
            System.out.println("Please enter a number from 1 to 5:");//The input data needs to be inside the loop, if it is outside, it will keep looping
            int a = sc. nextInt();
            switch (a) {<!-- -->
                case 1:
                    System.out.println("New");
                    break;
                case 2:
                    System.out.println("Open file");
                    break;
                case 3:
                    System.out.println("Save");
                    break;
                case 4:
                    System.out.println("Refresh");
                    break;
                case 5:
                    System.out.println("Exit");
                    break;
                default:
                    System. exit(0);
            }

        }
    }
}

1. Function description: Simulate the function of a calculator, perform addition, subtraction, multiplication, and division operations on two int-type data entered by the keyboard, and print the operation results

2. Requirements:

? (1) Enter three integers from the keyboard, the first two integers represent the data involved in the calculation,

? The third integer is the operation to be performed (0: indicates addition operation, 1: indicates subtraction operation, 2: indicates multiplication operation, 3: indicates division operation)

? (2) Use the knowledge learned today to complete the function

(3) The presentation format is as follows:

? Please enter the first integer: 30

? Please enter the second integer: 40

? Please enter the operation you want to perform (0: indicates addition operation, 1: indicates subtraction operation, 2: indicates multiplication operation, 3: indicates division operation): 0

? Console output: 30 + 40=70

import java.util.Scanner;

public class demo10 {<!-- -->
    public static void main(String[] args) {<!-- -->
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the first integer:");
        int a = sc. nextInt();
        System.out.println("Please enter the second integer:");
        int b = sc. nextInt();
        System.out.println("Please enter the third integer: 0 means addition operation, 1 means subtraction operation, 2 means multiplication operation, 3 means division operation");
        int i = sc. nextInt();

        switch (i) {<!-- -->
            case 0:
                System.out.println(a + " + " + b + "=" + (a + b));
                break;
            case 1:
                System.out.println(a + "-" + b + "=" + (a - b));
                break;
            case 2:
                System.out.println(a + "*" + b + "=" + (a * b));
                break;
            case 3:
                System.out.println(a + "/" + b + "=" + (a /b));
                break;
        }
    }
}
operation result:
Please enter the first integer:
30
Please enter a second integer:
40
Please enter the third integer: 0 for addition, 1 for subtraction, 2 for multiplication, 3 for division
0
30 + 40 = 70