package com.briup.day02;
/**
* 4.杨辉三角
*/
public class Triangle {
public static void main(String[] args) {
Triangle t=new Triangle();
t.test(7);//7行的正三角形
t.test2();
}
public void test(int line) {
for (int i = 0; i <= line; i++) {
for (int j = 0; j < line-i; j++) {
System.out.print(" ");//5个空格
}
for (int j = 0; j < (2*i-1); j++) {
System.out.print("✨");
}
System.out.println();
}
}
public void test2() {
int triangle[][]=new int[7][];
for (int i = 0; i < triangle.length; i++) {
triangle[i]=new int[i+1];
for (int j = 0; j < triangle.length-i; j++) {
System.out.print(" ");
}
for (int j = 0; j <= triangle[i].length-1; j++) {
if(i==0||j==0||j==triangle[i].length-1) {
triangle[i][j]=1;
}else {
triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1];
}
System.out.print(triangle[i][j]+" ");
}
System.out.println();
}
}
}
Java实现杨辉三角
最新推荐文章于 2024-08-29 18:44:48 发布