JAVA中三维倒三角金字塔怎么编,求解
发布网友
发布时间:2024-10-19 19:09
我来回答
共1个回答
热心网友
时间:2024-10-24 06:51
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Sum {
public static void main(String[] args) {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("请输入一个正整数按回车键:");
String numStr = bf.readLine();
int num = Integer.parseInt(numStr);
System.out.println(num);
for (int i = 0; i <= num; i++) {
for (int m = 1; m <= Math.abs(i); m++) {
System.out.print(" ");
}
for (int n = 1; n <= (num * 2 + 1) - 2 * Math.abs(i); n++) {
System.out.print("*");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println("请输入正整数!");
}
}
}