This is the 8th day of my participation in Gwen Challenge

This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details

Instances of 26

The title

Type the first letter of the week to determine the day of the week. If the first letter is the same, proceed to the second letter.

Analysis of the

You can tell the difference between Monday, Wednesday, and Friday by typing the first letter and then judging the difference between Tuesday and Thursday and Saturday and Sunday based on the second letter.

implementation

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @email : [email protected]
 * @website : https://cunyu1943.github.io
 * @date: 2021/6/3 * also@project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example26
 * @description: * /

public class Example26 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter initials");
        char first = scanner.next().toUpperCase().charAt(0);
        char second;
        switch (first) {
            case 'M':
                System.out.println("Monday");
                break;
            case 'W':
                System.out.println("Wednesday");
                break;
            case 'F':
                System.out.println("Friday");
                break;
            case 'T':
                System.out.println("Please enter the second letter.");
                second = scanner.next().toLowerCase().charAt(0);
                if (second == 'u') {
                    System.out.println("Tuesday");
                } else if (second == 'h') {
                    System.out.println("Thursday");
                }
                break;
            case 'S':
                System.out.println("Please enter the second letter.");
                second = scanner.next().toLowerCase().charAt(0);
                if (second == 'a') {
                    System.out.println("Saturday");
                } else if (second == 'u') {
                    System.out.println("Sunday");
                }
                break;
            default:
                break; }}}Copy the code

The results of

Examples of 27

The title

Find the prime number within 100

Analysis of the

Set a flag bit. The default flag is false to indicate a prime number. Once it is true, it indicates that the number is not a prime number.

implementation

/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @email : [email protected]
 * @website : https://cunyu1943.github.io
 * @date: 2021/6/4 he *@project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example27
 * @description: * /

public class Example27 {
    public static void main(String[] args) {
// The default is prime
        boolean flag = false;
        int count = 0;

        System.out.println("Prime numbers within 100:");
        for (int i = 2; i < 100; i++) {
            for (int j = 2; j <= Math.sqrt(i); j++) {
// If it can be divided completely, it is not a prime number
                if (i % j == 0) {
                    flag = true;
                    break;
                } else {
                    flag = false; }}// If it is prime, print and count, then print 5 per line
            if (flag == false) {
                count++;
                System.out.print(i + "\t");
                if (count % 5= =0) {
                    System.out.println();
                }
            }

        }
    }
}

Copy the code

The results of

Examples of 28

The title

Sort 10 numbers.

Analysis of the

You can use either the built-in arrays.sort () or bubbling sort method to sort 10 Arrays after the array.

implementation

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @email : [email protected]
 * @website : https://cunyu1943.github.io
 * @date : 2021/6/4 10:57
 * @project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example28
 * @description: * /

public class Example28 {
    public static void main(String[] args) {
        int[] arr = new int[10];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 10; i++) {
            System.out.println("Input control" + (i + 1) + "The number");
            arr[i] = scanner.nextInt();
        }
// call the built-in method directly
// Arrays.sort(arr);
// system.out.println (" Array sorted by built-in methods: "+ array.toString (arr));

// 2
        for (int i = 0; i < 10; i++) {
            for (int j = i + 1; j < 10; j++) {
                if (arr[i] > arr[j]) {
                    int tmp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp;
                }
            }
        }

        System.out.println("After bubble sort:"+ Arrays.toString(arr)); }}Copy the code

The results of

Examples of 29

The title

Find the sum of the diagonal elements of a 3 by 3 matrix.

Analysis of the

Define a two-dimensional array to hold the matrix elements, and then sum the diagonal elements (the diagonal elements are the same as the two-dimensional index).

implementation

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @email : [email protected]
 * @website : https://cunyu1943.github.io
 * @date : 2021/6/4 11:07
 * @project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example29
 * @description: * /

public class Example29 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[][] matrix = new int[3] [3];
        System.out.println("Input matrix elements (9)");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        System.out.println("The input matrix is:");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(matrix[i][j] + "\t\t");
            }
            System.out.println();
        }
// The sum of diagonal elements
        int sum = 0;

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (i == j) {
                    sum += matrix[i][j];
                }
            }
        }

        System.out.println("Sum of diagonal elements:"+ sum); }}Copy the code

The results of

Examples of 30

The title

Have a sorted array, now insert a number, the original rule is required to insert it into the array.

Analysis of the

Given an array sorted from smallest to largest, to insert a number, we simply copy the elements of the original array into a new array, then add the numbers to the array, and sort the new array!

implementation

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : cunyu
 * @version : 1.0
 * @email : [email protected]
 * @website : https://cunyu1943.github.io
 * @date: 2021/6/4 were *@project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example30
 * @description: * /

public class Example30 {
    public static void main(String[] args) {
        int[] arr = new int[] {1.4.12.23.43.66};
        Scanner scanner = new Scanner(System.in);

        System.out.println("The given array is:" + Arrays.toString(arr));
        System.out.println("Enter the element to insert");

        int value = scanner.nextInt();


        int[] newArr = new int[arr.length + 1];

        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
// Assign to the new array and sort
        newArr[arr.length] = value;
        Arrays.sort(newArr);
        System.out.println("The array after inserting elements is:"+ Arrays.toString(newArr)); }}Copy the code

The results of