下面是一种实现方式:

import java.util.Scanner;

public class StarTower {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入奇数:");
        int n = scanner.nextInt();

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2 * i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        for (int i = n - 2; i >= 0; i--) {
            for (int j = 0; j < n - i - 1; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2 * i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

运行结果截图:

输入:5

请输入奇数:5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

输入:7

请输入奇数:7
      *
     ***
    *****
   *******
  *********
 ***********
*************
 ***********
  *********
   *******
    *****
     ***
      *
实现程序输出下面这种星塔 在标准输入中输入 5 则输出上面这个 5 层星塔。如果输入 7 则输出 7 层星塔。假设输入参数都是奇数。你需要提交 java 代码及运行结果截图。任意选几个有代表性的输入即可

原文地址: http://www.cveoy.top/t/topic/jdLv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录