Java使用for()循环输出杨辉三角
1 package com.yzy.test; 2 3 public class Test { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 int[][] array = new int[10][]; 10 for (int i = 0; i < array.length; i++) { 11 array[i] = new int[i + 1]; 12 for (int j = 0; j <= i; j++) { 13 if (i == 0 || j == 0 || i == j) { 14 array[i][j] = 1; 15 } else { 16 array[i][j] = array[i - 1][j - 1] + array[i - 1][j]; 17 } 18 } 19 20 } 21 22 for (int[] is : array) { 23 for (int i : is) { 24 System.out.print(i + " "); 25 } 26 System.out.println(); 27 } 28 } 29 }
运行结果: