java day07 输出10行以内的杨辉三角

智商跟不上了,可能要缴纳税了

package helloworld;

import java.util.Scanner;

/**
 * 
 * @Description
 * @author 
 * @date 2020年7月24日上午9:52:54
 */
public class HelloWorld {
    public static void main(String[] args) {
    //1.声明初始化二维数组
    int[][] yanghui = new int[10][];
    
    //2.给创建的元素赋值
    for(int i = 0;i < yanghui.length;i++) {
    	yanghui[i] = new int[i+1];
    	
    	//2.1给首末元素赋值
    	yanghui[i][0] = yanghui[i][i] = 1;
    	//2.2给每行的非首末元素赋值
    	if(i > 1) {
    		for(int j = 1;j < yanghui[i].length - 1;j++) {
    			yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];
    		}
    	}
    }
    
    //3.遍历二维数组
    for(int i = 0 ;i < yanghui.length;i++) {
    	for(int j = 0;j < yanghui[i].length;j++) {
    		System.out.print(yanghui[i][j] + "  ");
    	}
    	System.out.println();
    }
    }
}
  

猜你喜欢

转载自blog.csdn.net/weixin_46381608/article/details/107564440