Java small demo-print

I remember when I first started learning JavaSE, I often used print statements to print different shapes. Today I wrote a few small demos, which are rectangles, squares, and triangles. In fact, the main exercise is the use of loop statements, which is helpful for subsequent array processing. You can also write more complex print statements.

package com.ispeasant.demo;

public class Demo1 {
        
        
    public static void main(String[] args) {
        
        


        printRectangle(4, 6);

        //printSquare(5);

        //printTriangle(5);


    }


    /**
     * 打印长方形
     * @param n:宽
     * @param m:长
     */
    public static void printRectangle(int n, int m) {
        
        
        for (int i = 0; i < n; i++) {
        
        
            for (int j = 0; j < m; j++) {
        
        
                System.out.print("*");
            }
            System.out.println();
        }
    }


    /**
     * 打印正方形
     * @param n:边长
     */
    public static void printSquare(int n) {
        
        
        for (int i = 0; i < n; i++) {
        
        
            for (int j = 0; j < n; j++) {
        
        
                System.out.print("*");
            }
            System.out.println();
        }
    }


    /**
     * n行三角形
     *
     * @param n:行数
     */
    public static void printTriangle(int n) {
        
        
        for (int i = 1; i <= n; i++) {
        
        
            for (int j = n; 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();
        }
    }


}

Guess you like

Origin blog.csdn.net/ISWZY/article/details/127855969