要求打印一个空心菱形
代码:
import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner src=new Scanner(System.in); System.out.println("你想要输出几行空心菱形?"); Integer size=src.nextInt(); if (size % 2==0){ size++; } //上半部分 for (int i = 0; i<size/2+1; i++) { for (int j =size/2+1;j>i+1 ; j--) { System.out.print(" "); } for (int j = 0; j <2*i+1; j++) { if (j==0||j==i*2) { System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } //下半部分 for (int j=size/2+1; j<size; j++) { for (int k =0;k<j-size/2; k++) { System.out.print(" "); } for (int i = 0; i <size*2-j*2-1; i++) { if(i==0||i==2*(size-j-1)){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } } }