2个回答
展开全部
import java.util.Scanner;
public class Test {
public void show(int n) {
int[][] nn = new int[n][]; // 根据输入值设置二维数组的第一维数
for (int i = 0; i < nn.length; i++) {
nn[i] = new int[i + 1]; // 设置第二维数
nn[i][0] = nn[i][i] = 1; // 对两头的元素赋值为1
for (int j = 1; j < nn[i].length - 1; j++) { // 对中间的元素赋值
nn[i][j] = nn[i - 1][j - 1] + nn[i - 1][j];
}
}
// 打印杨辉三角
for (int i = 0; i < nn.length; i++) {
for (int k = 0; k < n - i - 1; k++) {
System.out.print(" ");
}
System.out.print(nn[i][0] + " ");
for (int j = 1; j < nn[i].length; j++) {
if (nn[i][j] < 10) {
System.out.print(" " + nn[i][j] + " ");
} else if (nn[i][j] < 100) {
System.out.print(" " + nn[i][j] + " ");
} else {
System.out.print(" " + nn[i][j] + " ");
}
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入一个数字(n):");
int n = in.nextInt();
Test test = new Test();
test.show(n);
}
}
public class Test {
public void show(int n) {
int[][] nn = new int[n][]; // 根据输入值设置二维数组的第一维数
for (int i = 0; i < nn.length; i++) {
nn[i] = new int[i + 1]; // 设置第二维数
nn[i][0] = nn[i][i] = 1; // 对两头的元素赋值为1
for (int j = 1; j < nn[i].length - 1; j++) { // 对中间的元素赋值
nn[i][j] = nn[i - 1][j - 1] + nn[i - 1][j];
}
}
// 打印杨辉三角
for (int i = 0; i < nn.length; i++) {
for (int k = 0; k < n - i - 1; k++) {
System.out.print(" ");
}
System.out.print(nn[i][0] + " ");
for (int j = 1; j < nn[i].length; j++) {
if (nn[i][j] < 10) {
System.out.print(" " + nn[i][j] + " ");
} else if (nn[i][j] < 100) {
System.out.print(" " + nn[i][j] + " ");
} else {
System.out.print(" " + nn[i][j] + " ");
}
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入一个数字(n):");
int n = in.nextInt();
Test test = new Test();
test.show(n);
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class Test {
public static void main(String args[]) {
Scanner input =new Scanner(System.in);
System.out.println("输入n");
int n=input.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (j < i) {
a[i][j] = 1;
if (j == 0) {
a[i][j] = 1;
} else {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
} else {
a[i][j] = 1;
}
}
for (int i = 0; i < n; i++) {
for (int k = 1; k <= n - i; k++)
System.out.printf(" ");
for (int j = 0; j <= i; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.printf("\n");
}
}
}
public class Test {
public static void main(String args[]) {
Scanner input =new Scanner(System.in);
System.out.println("输入n");
int n=input.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (j < i) {
a[i][j] = 1;
if (j == 0) {
a[i][j] = 1;
} else {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
} else {
a[i][j] = 1;
}
}
for (int i = 0; i < n; i++) {
for (int k = 1; k <= n - i; k++)
System.out.printf(" ");
for (int j = 0; j <= i; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.printf("\n");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询