1 import java.util.Scanner; 2 3 public class EmptyDiamend11 { 4 public static void main(String args[]) { 5 Scanner input = new Scanner(System.in); 6 System.out.print("請輸入要打印拼成棱形上半個三角形的函數,rows="); 7 int rows = input.nextInt(); 8 int i, j, k; // 初始化變量 9 for (i = 1; i <= rows; i++) { 10 for (j = 1; j <= rows - i; j++) 11 System.out.print(" "); 12 for (k = 1; k <= 2 * i - 1; k++) 13 if (k == 1 || k == 2 * i - 1) 14 System.out.print("*"); 15 else 16 System.out.print(" "); 17 System.out.println(" "); 18 } 19 for (i = 1; i <= rows - 1; i++) { 20 for (j = 1; j <= i; j++) 21 System.out.print(" "); 22 for (k = 1; k <= 2 * (rows - i); k++) 23 if (k == 1 || k == 2 * (rows - i) - 1) 24 System.out.print("*"); 25 else 26 System.out.print(" "); 27 System.out.println(" "); 28 } 29 } 30 31 }
原文:http://www.cnblogs.com/fuyanan/p/3885428.html