杨辉三角的例子

package test.numble;

//杨辉三角
import java.util.Scanner;

class TestYangHuiSanJiao {
	public static void main(String[] args) {

		Scanner s = new Scanner(System.in);
		System.out.print("需要输出的行数:\n");
		int line = s.nextInt();
		for (int i = 1; i <= line; i++){// 行
		
			// 控制行首的空格数量
			for (int e = 1; e <= (line - i); e++) {
				System.out.print("  ");
			}
			for (int j = 1; j <= i; j++){// 每行的各个数值
			
				int num = 1;
				for (int k = 2; k <= j; k++) {
					num = num * (i - k + 1) / (k - 1);
				}
				// 控制数值之间空格数量 保持队形
				if (num / 1000 >= 1){
					System.out.print(num + " ");
				}else if (num / 100 >= 1){
					System.out.print(num + "  ");
				}else if (num / 10 >= 1){
					System.out.print(num + "   ");
				}else{
					System.out.print(num + "    ");
				}
			}
			System.out.println();
		}
	}
}
 

你可能感兴趣的:(例子)