1. 程式人生 > >【java】for迴圈輸出數字金字塔

【java】for迴圈輸出數字金字塔

輸出下列數字金字塔。
    1
  121
 12321
1234321

複製程式碼
 1 public class deng {
 2 public static void main(String args[])
 3 {
 4     int n,i,k,j;                    
 5     for(n=1; n<=4; n++) //n在最外層控制行數 
 6     {
 7         for(i=1; i<=4-n;i++)
 8             System.out.print(" ");
 9         for(k=1; k<=n; k++)
10 System.out.print(k); 11 for(j=n-1; j>=1;j--) 12 System.out.print(j); 13 14 System.out.print("\n"); 15 16 } 17 } 18 }