Java打印杨辉三角
/** * 打印杨辉三角 功能描述:使用多重循环打印6阶杨辉三角 * @author Administrator * */ public class sz_7 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入行号:"); int m = in.nextInt(); int n = 2*m-1;//列元素数; int arr[][] = new int[m][n]; for (int i = 0; i < m; i++) { //外循环控制行 for (int j = 0; j < n; j++) { //内循环控制列 if (j<(m-i-1)||(j>=(m+i))) { //输出等腰三角形两边空格 System.out.print(" "); }else if (j==(m-i-1)||j==(m+i-1)) { //计算输出等腰三角形两边的空格 arr[i][j] = 1; System.out.print(arr[i][j]); }else if ((i+j)%2==0&&m%2==0||(i+j)%2==1&&m%2==1) { System.out.print(" "); }else { arr[i][j] = arr[i-1][j-1]+arr[i-1][j+1]; System.out.print(arr[i][j]); } } System.out.println(); } } }