Java instance - print graphics

Keywords: Java

Java instance – print diamond

Outputs a diamond with a specified number of rows.

package com.example.yan.java Print graphics;

public class Main {

    public static void main(String[] args) {
        print(8); // Output 8-line diamond
    }

    public static void print(int size) {
        if (size % 2 == 0) {
            size++; // Calculate diamond size
        }
        for (int i = 0; i < size / 2 + 1; i++) {
            for (int j = size / 2 + 1; j > i + 1; j--) {
                System.out.print(" "); // Output the blank space in the upper left corner
            }
            for (int j = 0; j < 2 * i + 1; j++) {
                System.out.print("*"); // Output diamond top half edge
            }
            System.out.println(); // Line feed
        }
        for (int i = size / 2 + 1; i < size; i++) {
            for (int j = 0; j < i - size / 2; j++) {
                System.out.print(" "); // The lower left corner of the output diamond is blank
            }
            for (int j = 0; j < 2 * size - 1 - 2 * i; j++) {
                System.out.print("*"); // Bottom edge of output diamond
            }
            System.out.println(); // Line feed
        }
    }
}

Manually enter the presentation picture

public static void main(String[] args) {
        while (true){
            Scanner scanner = new Scanner(System.in);
            System.out.println("************Please enter the number of diamonds************");
            String n = scanner.nextLine();
            System.out.println("The printed graphics are as follows================");
            print(Integer.valueOf(n));

        }
    }

    public static void print(int n){
        if (n % 2 == 0) {
            n++;//Calculate the number of rows of the diamond
        }

        for (int i = 0; i < n; i++){
            int start;//Line i begins with a diamond subscript
            int end;//Draw a diamond subscript at the end of line i
            if (i <= n/2){//Calculate the start and end subscript of the increment part
                start = n/2-i;
                end = n/2+i;
            }else {//Calculate the start and end subscript of the decrement part
                start = n/2 - (n-1-i);
                end = n/2 + (n-1-i);
            }

            int count = 0;
            for (int j =0; j<n;j++){
                if (j>=start && j<=end){//The drawing part is between the beginning and the end
                    System.out.print("*");
                    count++;
                    if (count == end - start +1){
                        break;
                    }
                }else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

    }

Java instance – 99 multiplication table

Output 99 multiplication table.

public class MultiplicationTable {
    public static void main(String[] args) {
        for(int i=1;i<=9;i++) {
            for(int j=1;j<=i;j++) {
                System.out.print(j+"×"+i+"="+i*j+"\t");// \Tskips to the next TAB position
            }
            System.out.println();
        }
    }
}

Java instance – print triangles

First, determine that our output is:


So how can we do this?

1. Firstly, the structure of graphics is analyzed

We can see that there are 5 lines in the graph. Then, can we create a for loop statement to control it in 5 lines? The answer is yes.

for(int i = 1 ;i <= 5 ;i++ ){
 
}

In this way, we create a for loop code block that loops 5 times, which is the outermost loop.

2. Then, analyze how the graph is composed. We can divide the graph into the following parts:

caption

We can split the graph into three triangles.

3. Create No. 1 blank triangle

You can see that the first line outputs 4 spaces, the second line outputs 3 spaces, the third line outputs 2, the fourth line outputs 1, and the fifth line does not

From this law, we can see that it is a law of decreasing in turn, so how to realize it?

We can imagine that from 1 to 5, there are four numbers in the middle; There are three numbers from 2 to 5, from 3 to 5

Can we use this principle? The answer is of course. So how? See code:

for(int i = 1;i<=5 ;i++) {
    for(int j = 5; j >= i ; j--)//Create Figure 1
        System.out.print(" ");
    System.out.println();
}

The first for statement is the five loop statement just defined

The second for loop, let's parse it:

First, define a j variable of type int and assign a value of 5 to J

Then we think that since we want to shorten the distance, each cycle j is - 1, which just meets our requirements:

The first large cycle i=1, j=5, so it meets the condition of J > = I, and then outputs a space, and then j-1. Now the value of J is 4, which meets the condition of J > = I, and then outputs

......

Until J = 0, J > = I does not comply, jump out of the inner loop

Now go to System.out.println(); Line feed

Now go back to the outer loop I + +, I becomes 2, conforming to I < = 5, and enter the inner loop

Define J = 5, J > = I, match, output a space, j-1

j is now 4, j > = I, yes, output a vacancy, j-1

......

Until J = 1, J > = I, not established, jump out of the internal training, and then change the line

Then i+1, and then enter the internal circulation

In this way, a four line inverted triangle is formed, and No. 1 pattern is formed.

4. The principle of establishing Figure 2 is exactly the same as that of Figure 1, but it is just the opposite

for(int i = 1 ;i<=5 ;i++){
    for(int j = 5; j >= i ; j--)//Create Figure 1
        System.out.print(" ");
    for(int j = 1; j <= i; j++)//Create Figure 2
        System.out.print("*");
    System.out.println();
}

If the establishment of Figure 1 is the same, you can understand it by yourself. In this way, figure 2 is established

5. Create Figure 3

for(int i = 1; i <= 5; i++){
    for(int j = 5 ;i <= j; j--)//Create Figure 1
        System.out.print(" ");
    for(int j = 1; j <= i; j++)//Create Figure 2
        System.out.print("*");
    for(int j = 1; j < i; j ++)//Create Figure 3
        System.out.print("*");
 
}

Similarly, like No. 1 and No. 2, the principle of establishing No. 3 figure is the same

But please note that figure 3 is not output in the first line, so you should cut it off in the first big cycle and let it output in the second big cycle

Therefore, the judgment condition this time is j < I, minus equals.

class Demo{
    public static void main(String[] args){
        for(int i=1;i<=5;i++){
            for(int j=5; i<=j; j--)
                System.out.print(" ");
            for(int j=1; j<=i; j++)
                System.out.print("*");
            for(int j=1; j<i; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Java instance – print inverted triangles

Print inverted triangles.

package com.example.yan.java Print graphics;

public class Main3 {
    public static void main(String[] args) {
        //Print inverted triangles
        for (int m = 1; m <= 4; m++) {
            //Print spaces
            for (int n = 0; n <= m; n++) {
                System.out.print(" ");
            }
            //Print*
            for (int x = 1; x <= 7 -2 * (m - 1); x++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Java instance – print parallelogram

Output parallelogram.

package com.example.yan.java Print graphics;

public class Parallelogram {
    public static void main(String[] args) {
        //One at a time in the outer loop*
        for (int i = 1; i <= 5; i++) {
            //Fill in spaces
            for (int j = 1; j <= 5 - i; j++) {
                System.out.print(" ");
            }
            //The inner loop prints one at a time*
            for (int k = 1; k <= 5; k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Java instance - print rectangle

  public static void main(String[] args) {
         //The outer loop outputs one line at a time*
        for (int i = 1; i <= 5; i++) {
            System.out.print("*");
            //The inner loop outputs one at a time*
            for (int j = 1; j <= 5; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

Code cloud source address: https://gitee.com/yan_wen_chao/java-string-operation

I hope it can be helpful to you; Study together;

Posted by andrewburgess on Sun, 28 Nov 2021 10:53:42 -0800