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 }

运行结果:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM