java outputs various triangles (*)

package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= 5; i++) {

                System.out.print("* ");}
                System.out.println();
            }
        }
    }

Recently, I'm trying to do some basic problems when I first started learning JAVA. At the same time, I'm also gnawing CS61B from the beginning.
After a long time of precipitation, the content of many things is very different from that of the past.
But I'm ashamed that my ability is not good, so some simple questions also took a lot of time, so I started to write down some contents.

The code above comes from my first time doing HW0, which is an assignment assigned by the teacher.

Output triangles (many types of triangles are derived)

  1. Taking the first code block as an example, the output is not a triangle, but a rectangle with a specification of (length x width): 9x5.
  2. Isosceles right triangle (same length and width)
  3. an isosceles triangle
  4. Inverted triangle
  5. Up and down triangle

I:

package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= 9; j++) { //for loop traversal. This j represents the number of output rows
            for (int i = 1; i <= 5; i++) {//Nested loop. This i represents the number of output columns. Here, there are 5 columns in a row*
				 System.out.print("* ");}
                System.out.println();
            }
        }
    }

2: (to the left)
When it is different from the output rectangle, if you want to output isosceles right triangle, you should control the number of * in each row and column to be the same as the number in this row.
Therefore, the j normal loop represents the number of rows of the output triangle.
Then make sure that i is consistent with j, so in the nested loop, i will be compared with j.

package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {//The loop i in the nest is compared with j. when j outputs 1, i also outputs 1, ensuring that the number of * in each row and column is equal to the corresponding number of the row.

                System.out.print("* ");}
                System.out.println();
            }
        }
    }

Another case is the isosceles right triangle on the right. My initial idea is that it can be understood as outputting a containing (space)+ 🌟 Rectangle of:
Take the side length as 3 distance, the first line has 2 spaces + 1 * sign, and so on.
But the output result is like this: a slash is output, and there is an empty line here, which should be the reason for println.

package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= ; j++) {
            for (int i = 1; i <= 3-j; i++) {

                System.out.print(" ");}
            System.out.print("*");
                System.out.println();
            }
        }
    }
   

After correction:

package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= 3; j++) {
            for (int i = 1; i <= 3-j; i++) {
                System.out.print(" ");
            }
            for (int i = 1; i <= j; i++) {
                System.out.print("*");//Added after the empty number*
            }
            System.out.println();
        }
    }

}

III:

package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= 4; j++) {
            for (int i = 1; i <= 4-j; i++) {
                System.out.print(" ");
            }
            for (int i = 1; i <= j; i++) {
                System.out.print("*");
            }
            for (int x =1; x <= j-1;x++) {//Isosceles triangle, add an isosceles right triangle one line smaller than the original from the second line
                System.out.print("*");//Continue output from space followed by
            }
            System.out.println();
        }
    }

}

result:

IV:
Inverted triangle can also be understood from the perspective of rectangle.
It can be divided into right inverted triangle, left inverted triangle and isosceles inverted triangle. (take all four behaviors as examples)
The right inverted triangle is:

package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= 4; j++) {
            for (int i = 4; i >=j ; i--) {
                System.out.print("*");
            }
           /** for (int x = 1; x <= j; x++) {
                System.out.print(" ");
            }
**/ //No spaces are required here
            System.out.println();
        }
    }

}

The left inverted triangle is:

package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= 4; j++) {
          for (int x = 2; x <= j; x++) {
                System.out.print(" ");//x starts from 2, indicating that the traversal cycle is carried out on the second line, and the space is output, and each line is incremented in turn
            }
            for (int i = 4; i >=j ; i--) {
                System.out.print("*");//Output * after the space to find out the numerical relationship of these symbols
            }
            System.out.println();
        }
    }

}

Isosceles inverted triangle:

[episode: when writing the code, the output parallelogram is written by mistake, and the code is as follows (parallelogram)]
Is a rectangular variant

package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= 4; j++) {
          for (int x = 2; x <= j; x++) {
                System.out.print(" ");
            }//The whole output is an isosceles triangle on the right
            for (int i = 1; i <=3 ; i++) {//Rectangular variant 4x3
                System.out.print("*");
            }
            System.out.println();
        }
    }

}

Isosceles inverted triangle:

 package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= 4; j++) {
          for (int x = 2; x <= j; x++) {
                System.out.print(" ");
            }
            for (int i = 4; i >=j ; i--) {
                System.out.print("*");
            }//Output a right inverted triangle
           for(int a=3;a>=j;a--){
               System.out.print("*");
           }//Another small-scale left inverted triangle (3 lines)
            System.out.println();
        }
    }

}

result:

5: Upper and lower double triangle

package triangle;

public class triangle {
    public static void main(String[] args) {
        for (int j = 1; j <= 4; j++) {
            for (int x = 3; x >= j; x--) {
                System.out.print(" ");
            }
            for (int i = 1; i <= j; i++) {
                System.out.print("*");
            }
            for (int x =1; x <= j-1;x++) {//Isosceles triangle, add an isosceles right triangle one line smaller than the original from the second line
                System.out.print("*");//Continue output from space followed by
            }
            System.out.println();
        }
        for(int a=1;a<=3;a++){
            for(int b=1;b<=a;b++){
                System.out.print(" ");
            }
            for(int c=3;c>=a;c--){
                System.out.print("*");
            }//The law of decrement is that columns should be compared with rows, rather than simple numerical comparison. If C = 3 is a parallelogram.
            for(int c=1;c>=a-1;c--){
                System.out.print("*");
            }//Here is a triangle with the original scale (4 rows) small twice.
            System.out.println();
        }
    }
}

result:

Finally, this code is too miscellaneous. It has always been nested in for loops. Later, I will look for simple structure optimization code.

Keywords: data structure

Added by nakkaya on Sat, 12 Feb 2022 17:40:25 +0200