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