用Java实现杨辉三角形打印器功能
实验代码
printmess .java
package lab1;
import java.util.Scanner;
public class printmess {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("欢迎来到打印区,请给出你的选择:A或者B");
String line = sc.nextLine();
switch (line) {
case "A":
asd();
break;
case "B":
codn();
break;
}
}
}
public static void asd() {
System.out.println("**********************");
}
public static void codn() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你希望的杨辉三角形行数:");
int a = sc.nextInt();
System.out.println("杨辉三角图形如下:");
int[][] arr = new int[a][a];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}