金字塔是继99乘法表决之后的一个经典图形排列题
题目要求:
要求用户可以自主控制塔身的层数, 完成如下金字体样式;
输入格式:
4
输出格式:
*
***
*****
*******
输入样例:
在这里给出一组输入。例如:
5
8
输出样例:
在这里给出相应的输出。例如:
*
***
*****
*******
*********
*
***
*****
*******
*********
***********
*************
***************
代码长度限制16 KB
时间限制4000 ms
内存限制24 MB
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
//记录金字塔个数
int count = 0;
while(sc.hasNext()){
//获取金字塔层数
int num = sc.nextInt();
//生成金字塔
result(num, count);
count++;
}
}
public static void result(int num, int count){
//第二个金字塔开始先打印两行回车进行分割
if(count != 0) System.out.println("\n");
for(int i = 0; i < num; i++){
//打印金字塔空格部分
for(int j = 0; j < num - i - 1; j++){
System.out.print(" ");
}
//打印金字塔结构部分
for(int j = 0; j < 1 + i * 2; j++){
System.out.print("*");
}
System.out.println();
}
}
}
![](https://img-blog.csdnimg.cn/img_convert/679d4e09bb49b28eb6f1577f0026a52e.png)