java语言用*输出一个下三角和上三角结合的图形,就像漏斗一样
发布网友
发布时间:2022-05-10 08:42
我来回答
共2个回答
热心网友
时间:2023-11-24 18:45
public static void print(int n) {
// 打印倒三角
for (int i = 0; i < n; i++) {
// 先打印空格
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// 打印*
for (int j = 0; j < n - i; j++) {
System.out.print("* ");
}
System.out.println();
}
// 打印正三角
for (int i = 0; i < n; i++) {
// 先打印空格
for (int j = 0; j < n-i-1; j++) {
System.out.print(" ");
}
// 打印*
for (int j = 0; j < i+1; j++) {
System.out.print("* ");
}
System.out.println();
}
}
热心网友
时间:2023-11-24 18:45
按照你的要求编写的程序如下:
public class C {
public static void main(String[] args) {
final int N=4;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print(" ");
}
for (int k = 1; k <=2*N-2*i+1 ; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= N-1; i++) {
for (int j = 1; j <=N-i ; j++) {
System.out.print(" ");
}
for (int k = 1; k <=2*i+1 ; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
运行结果:
*******
*****
***
*
***
*****
*******