Java Double Loop Printing Patterns

Links to the original text: https://blog.csdn.net/yajing8/article/details/73548896

Using Java double loop to print various pattern summaries: the outer loop controls the number of rows, which is fixed, and the inner loop controls the output of each row. When writing the conditions for the inner loop, it is necessary to find the relationship between the number of graphics output from each layer and the outer loop. There must be a certain relationship, either a sum or a certain mathematical relationship. Examples are as follows: using java to print right triangle, chamfer triangle, isosceles triangle, parallelogram, rectangle, solid diamond, four nine multiplication tables:

package BiShiTest;

import java.util.Scanner;

import org.junit.Test;
/**
 * Double Loop Exercise: Printing Various Graphics and Printing Four Nine-Nine Multiplications
 */
public class PrintGraphTest {
    Scanner scanner = new Scanner(System.in);

    //1. Print rectangle
    @Test
    public void testOne(){
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    //2. Print right triangle
    @Test
    public void testTwo(){
        System.out.print("Please enter the number of rows of a right triangle:");
        int row = scanner.nextInt();
        for (int i = 1; i <=row ; i++) {
            for (int j = 1; j <= (2*i-1); j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    //3. Print chamfer triangle
    @Test
    public void testThree(){
        System.out.println("Please enter the number of rows of a right triangle:");
        int count = scanner.nextInt();
        for (int i = 1; i <= count; i++) {
            for (int j = 1; j <= count-i+1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    //4.1 Print isosceles triangle
    @Test
    public void testFour(){
        System.out.println("Please enter the number of rows in an isosceles triangle:");
        int row = scanner.nextInt();
        for (int i = 1; i <= row; i++) {
            for (int j = 1; j <=(2*row-1); j++) {
                if (j<=row-i) {
                    System.out.print(" ");
                }
                else if (j<(row+i)) {

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

    //4.2 Printing isosceles triangle method II
    @Test
    public void testFour2(){
        System.out.print("Please enter the number of rows in an isosceles triangle:");
        int row = scanner.nextInt();
        for (int i = 1; i <= row; i++) {
            //Print the left space first
            for (int j = 0; j < row-i; j++) {
                System.out.print(" ");
            }
            //Print *
            for (int j = 1; j <= 2*i-1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }


    //5. Print parallelogram
    @Test
    public void testFive(){
        System.out.print("Please enter the number of rows of a parallelogram:");
        int row = scanner.nextInt();
        for (int i = 0; i < row ; i++) {
            for (int j = 1; j <= 2*row; j++) {
                if (j<=row-i) {
                    System.out.print(" ");
                }else if (j<=2*row-i) {
                    System.out.print("#");

                }
            }
            System.out.println();
        }
    }

    //6. Print diamond
    @Test
    public void testSix(){
        System.out.println("Please enter the number of diamond rows:");
        int row = scanner.nextInt();
        //Positive triangle
        for (int i = 1; i <=row/2+1; i++) {
            for (int j = 0; j<(row/2+1-i); j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <=2*i-1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        //Inverted triangle
        for (int i = row/2; i >=1; i--) {
            for (int j = 1; j<=row/2-i+1; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <=2*i-1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }




    //7. Print the 99 multiplication table in the lower left corner
    @Test
    public void testSeven(){
        for (int i = 1; i <=9 ; i++) {
            for (int j = 1; j <=i; j++) {
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
            }
            System.out.println();
        }
    }

        //8. Print the 99 multiplication table in the form of upper left triangle
        @Test
        public void testEight(){
            for (int i = 9; i >=1 ; i--) {
                for (int j = 1; j <=i; j++) {
                    System.out.print(j+"*"+i+"="+(j*i)+"\t");
                }
                System.out.println();
            }
        }

    //9. Print the 99 multiplication table in the upper right corner
    @Test
    public void testNine(){
          for (int i=9; i>=1; i--) {
              for (int j= 9; j>=1; j--) {
                  if (j>i) {
                      System.out.print("\t");
                  }else {
                      System.out.print(i+"*"+j+"="+i*j+"\t");
                  }   
              }
              System.out.println();
             }
        }

        //9. Print the 99 multiplication table in the lower right corner
        @Test
        public void testTen(){
          for (int i=1; i<=9; i++) {
              for (int j= 9; j>=1; j--) {
                  if (j>i) {
                      System.out.print("\t");
                  }else {
                      System.out.print(i+"*"+j+"="+i*j+"\t");
                  }   
              }
              System.out.println();
           }
        }

}

Keywords: Java Junit

Added by doofystyle on Fri, 04 Oct 2019 18:37:15 +0300