import java.util.Scanner;
public class towel{
public static void main(String args[]){
System.out.println("请先输入金字塔的高度h:");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int no=0;
int x=0;
int y=0;
for(x=1;x<=n;x++){
for(no=n-x;no>0;no--){
System.out.println(" ");
}
for(y=1;y<=(x-1)*2+1;y++){
System.out.print("*");
}
System.out.println();
}
}
}
上面是我的代码,我想输出金字塔,但是每一行都是从左边开始,怎么从中间开始
每行前面加空格啊
import java.util.Scanner;
public class Towel {
public static void main(String args[]) {
System.out.println("请先输入金字塔的高度h:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int no = 0;
int x = 0;
int y = 0;
for (x = 1; x <= n; x++) {
for (no = n - x; no > 0; no--) {
System.out.print(" ");
}
for (y = 1; y <= (x - 1) * 2 + 1; y++) {
System.out.print("*");
}
System.out.println();
}
}
}