0
点赞
收藏
分享

微信扫一扫

案例 打印星星金字塔

米小格儿 2024-07-24 阅读 35

//      *
//     ***
//    *****
//   *******
//  *********
// ***********

package com.lzk.test;


import java.util.Scanner;

public  class test {
    //      *
    //     ***
    //    *****
    //   *******
    //  *********

    //空格    *    空格
    //4      1     4
    //3      3     3
    //2      5     2
    //1      7     1
    //0      9     0
    public static void main(String[] args) {
        //输入打印几行
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入打印几行星星:");
        int n = sc.nextInt();

        printStars(n);
    }

    public static void printStars(int n) {
        //打印n行

        for (int i = 1; i <= n; i++) {
            //打印空格
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            //打印星号
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            //打印空格
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            System.out.println();

        }


    }
}

举报

相关推荐

0 条评论