This is the fifth 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

Examples of 16

The title

Output the 9 * 9 formula.

Analysis of the

Direct two layer loop can, should pay attention to newline!

implementation

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

public class Example16 {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + (i * j) + "\t\t"); } System.out.println(); }}}Copy the code

The results of

Examples of 17

The title

Monkey eat peach problem: the monkey picked a number of peaches on the first day, immediately ate the general, is not satisfied, and eat one more, the next morning will be the rest of the peach half, and eat one more. For the rest of the day, I ate half and one each morning. In the morning of the 10th day, when I wanted to eat again, THERE was only one peach left. How much did you pick the first day?

Analysis of the

If you think about it backwards, from day 10, to day 1, the number of peaches eaten per day is (sum+1) * 2, where sum is (sum+1) * 2 of the next day, then the number of days in the cycle is nine, because from one to ten, only nine days have passed.

implementation

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

public class Example17 {
    public static void main(String[] args) {
        int sum = 1;
        for (int i = 2; i <= 10; i++) {
            sum = (sum + 1) * 2;
        }

        System.out.println("Number of peaches on day 1:"+ sum); }}Copy the code

The results of

Examples of 18

The title

There are three players in each of the two ping-pong teams. Team A has three players in A, B and C, and team B has three players in X, Y and Z. Known a and X ratio, C and X, Z ratio, the competition list!

Analysis of the

Match a, B, and C with x, y, and Z, and then exclude the combinations that do not fit the question. The final result is the list of matches. Mainly use for loop and if condition judgment to achieve!

implementation

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

public class Example18 {
    static char[] teamA = {'a'.'b'.'c'};
    static char[] teamB = {'x'.'y'.'z'};

    public static void main(String[] args) {
        int size = teamA.length;

        System.out.println("The battle list is as follows:");
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (teamA[i] == 'a' && teamB[j] == 'x') {
                    continue;
                } else if (teamA[i] == 'a' && teamB[j] == 'y') {
                    continue;
                } else if ((teamA[i] == 'c' && teamB[j] == 'x') || (teamA[i] == 'c' && teamB[j] == 'z')) {
                    continue;
                } else if (((teamA[i] == 'b' && teamB[j] == 'z') || (teamA[i] == 'b' && teamB[j] == 'y'))) {
                    continue;
                } else {
                    System.out.println(teamA[i] + " VS " + teamB[j]);
                }
            }

        }
    }
}
Copy the code

The results of

Example 19

The title

Implement printing out a diamond.

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
Copy the code

Analysis of the

Divide the diamond into upper and lower parts and then print Spaces and *.

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 13:30
 * @project: Java programming instance *@package : PACKAGE_NAME
 * @className : Example19
 * @description: * /

public class Example19 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number of lines to print");
        int row = scanner.nextInt();

        if (row % 2= =0) {
            // Calculate the size of the diamond and divide it into upper and lower parts
            row++;
        }
        for (int i = 0; i < row / 2 + 1; i++) {
            for (int j = row / 2 + 1; j > i + 1; j--) {
                // Prints the white space in the upper left corner
                System.out.print("");
            }
            for (int j = 0; j < 2 * i + 1; j++) {
                // Output the upper edge of the diamond
                System.out.print("*");
            }
            System.out.println(); / / a newline
        }
        for (int i = row / 2 + 1; i < row; i++) {
            for (int j = 0; j < i - row / 2; j++) {
                // Output diamond bottom left blank
                System.out.print("");
            }
            for (int j = 0; j < 2 * row - 1 - 2 * i; j++) {
                // Output the lower edge of the diamond
                System.out.print("*");
            }
            / / a newlineSystem.out.println(); }}}Copy the code

The results of

Examples of 20

The title

There is a sequence of fractions: 2/1, 3/2, 5/3, 8/5… , find the sum of the first 20 terms of the sequence.

Analysis of the

Observe the sequence, from the second term, the numerator of the current fraction is equal to the sum of the numerator and denominator of a fraction, the denominator is equal to the numerator of a fraction, according to this rule, the sequence of fractions can be summed up!

implementation

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

public class Example20 {
    public static void main(String[] args) {
        double sum = 0.0 d;
        / / the denominator
        int denominator = 1;
        / / molecular
        int numerator = 2;
        for (int i = 1; i <= 20; i++) {
            sum = sum + (double) numerator / denominator;
            int tmp = denominator;
            denominator = numerator;
            numerator = tmp + denominator;
            System.out.println("Before" + i + "Sum of terms:"+ sum); }}}Copy the code

The results of