打印金字塔、空心金字塔、空心菱形

//打印金三角
/*
        *               1         2*i+1 4 line-i 5
       ***              3         2*i+1 3 line-i 4 6
     *****             5         2*i+1 2 line-i 3 7
    *******           7         2*i+1 1 line-i 2 8
   *********         9         2*i+1 

 public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入行数:");
        int line = in.nextInt();
        for (int i = 1; i <=line; i++) {
            for (int k =1; k<=line-i ; k++) {
                System.out.print(" ");
            }
            for (int j = 1; j <=2*i-1 ; j++) {
                 System.out.print("*");
            }
            System.out.println();
        }
    }
}

打印金字塔、空心金字塔、空心菱形_第1张图片

//打印空心金三角
/*
        *               1         2*i+1 4 line-i 5
       ***              3         2*i+1 3 line-i 4 6
     *****             5         2*i+1 2 line-i 3 7
    *******           7         2*i+1 1 line-i 2 8
   *********         9         2*i+1 

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入行数:");
        int line = in.nextInt();
        for (int i = 1; i <=line; i++) {
            for (int k =1; k<=line-i ; k++) {
                System.out.print(" ");
            }
            for (int j = 1; j <=2*i-1 ; j++) {
                if(j==1||j==2*i-1||i==line){
                    System.out.print("*");
                }else {
                    System.out.print(" ");
                }
               }
                System.out.println();
        }
    }
}

打印金字塔、空心金字塔、空心菱形_第2张图片

现在开始做下三角,注意只是上三角的逆置,不需要从上图第6行开始考虑,这点不太清楚的,多调试,就行了



import java.util.Scanner;

public class demo6 {
    //打印中空金三角
    /*
          *           1   2*i+1     4   line-i    5
         ***          3   2*i+1     3   line-i   4 6
        *****         5   2*i+1     2   line-i   3 7
       *******        7   2*i+1     1   line-i   2 8
      *********       9   2*i+1     0
       *******       1   1
        *****        2   2
         ***         3   3
          *          4   4
     */
    public static void main(String[] args) {
        int line = 0;
        Scanner in = new Scanner(System.in);
        System.out.println("请输入你要输出的菱形层数:");
        line = in.nextInt();
        for (int i = 1; i <=line; i++) {
            for (int k = 1; k <=line-i ; k++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2*i-1; j++) {
                if(j==1||j==2*i-1){
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println(" ");
        }
        for (int i = line; i >= 1; i--) {
            for (int m = 0; m <= line-i; m++) {
                System.out.print(" ");
            }
            for (int n = 2*(i-1)-1; n > 0; n--) {
                if(n==(2*(i-1)-1)||n==1) {
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println(" ");
        }

    }
}

打印金字塔、空心金字塔、空心菱形_第3张图片

感兴趣的小伙伴,可以尝试做,实心菱形,圣诞树·······

你可能感兴趣的:(韩顺平java习题,java)