Java实现打印多层空心菱形
作者:互联网
package text;
import java.util.Scanner;
public class text1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入要打印几行的实心菱形:");
int n=sc.nextInt(); //n为菱形上半部分的行数
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
System.out.print(" ");
}
for(int j=0;j<2*i+1;j++) {
if(i==n-1 && j==n-1 )
{
System.out.print(" ");
}
else
{
System.out.print("*");
System.out.print(" ");
}
}
System.out.println();
}
for(int i=n-2;i>=0;i--) {
for(int j=i+1;j<n;j++) {
System.out.print(" ");
}
for(int j=0;j<2*i+1;j++) {
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
}
}
运行结果:
标签:Java,Scanner,int,System,空心,println,菱形,print,out 来源: https://blog.csdn.net/qq_46163139/article/details/118634233