不同样式输出菱形

不同样式输出菱形

1、实心菱形

package mrzhangxd.xyz.demo;

/**
 * CopyRight (C),2019
 *
 * @Package: mrzhangxd.xyz.demo
 * @Name: Test
 * @Auther: zhangxiaodong3
 * @Creatte: 2019/12/19
 * @Description: Output 7 lines of solid diamonds
 */

/*
Output style:
    *
   ***
  *****
   ***
    *
Main idea:

    1. Divide 7 rows of diamonds into 4 and 3 columns for output;

    2.The first half
        "*" Quantity per line: 2 * i-1
        Number of spaces per line: line-i
    3. The lower part:
        Number of spaces per line: i
        "*" Quantity per line: 2 * i-1
 */
public class Test {

    public static void main(String[] args){

        // The number of input lines is L, and the output diamond is (2L-1)
        int L = 4;

        // The first half of the output (4 lines)
        for(int i = 1; i <= L; i++){

            // Output the space of each line in the top half
            for(int j = 1;j <= (L - i); j++){

               System.out.print(" ");

            }

            // Output the "*" sign of each line in the lower half
            for(int j = 1; j <= (2*i)-1; j++){

                System.out.print("*");

            }

            System.out.println();
        }


        // The lower part of the output (3 lines)
        for(int i = 1; i <= (L-1);i++){

            // Output spaces for each line in the lower half
            for(int j = 1;j <= i;j++){

                System.out.print(" ");
            }

            // Output "*" sign for each line in the lower half
            for(int j = 1; j <= (2*(L-i)-1);j++){

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

            System.out.println();
        }

    }

}

2、空心菱形

package mrzhangxd.xyz.demo;

/**
 * CopyRight (C),2019
 *
 * @PackageName: mrzhangxd.xyz.demo
 * @ClassName: Test2
 * @Auther: zhangxiaodong3
 * @Created: 2019/12/1915:51
 * @Description: Output 7 rows of hollow diamonds...
 */
/*
Output style:
     *
    * *
   *   *
    * *
     *

Main idea:

    1. Divide 7 rows of diamonds into 4 and 3 columns for output;

    2.The first half
        "*" Quantity per line: 2 * i-1
        Number of spaces per line: line-i
    3. The lower part:
        Number of spaces per line: i
        "*" Quantity per line: 2 * i-1
    4. Consider where to output spaces and where to output "*"
        Set the line number to "n", then the position of the output "*" is:
        (j == 1 | j == ((2 * n) -1))

 */
public class Test2 {

    public static void main(String[] args){

        // The number of input lines is L, and the output diamond is (2L-1)
        int L = 4;

        // Determine the number of rows in the upper half of the output diamond
        for(int i = 1;i <= L; i ++){

            // Output the number of spaces per line
            for(int j = 1;j <= (L-i);j++){

                System.out.print(" ");
            }


            // Output the number of "*" in each line
            for(int j = 1;j <= ((2*i)-1);j++){


                // Output "*" in the if condition, otherwise output spaces
                if(j == 1 || j == ((2*i)-1)){

                   System.out.print("*");

                }else{

                    System.out.print(" ");

                }
            }

            System.out.println();

        }

        // Determine the number of rows of the output diamond in the lower half
        for(int i = 1;i <= (L-1);i++){


            // Output the number of spaces per line
            for(int j = 1;j <= i;j++){

                System.out.print(" ");

            }

            // Output the number of "*" in each line
            for(int j = 1;j <= (2*(L-i)-1);j++){


                // Output "*" in the if condition, otherwise output spaces
                if(j == 1 || j == 2*(L-i)-1){

                    System.out.print("*");

                }else{

                    System.out.print(" ");
                }


            }

            System.out.println();
        }

    }

}

PS:上代码就可以了,思路代码注解里有...

原文地址:https://www.cnblogs.com/MrZhangxd/p/12069177.html