我得到了一个驱动程序/类的示例,在尝试在 Java 上找到答案之前,我应该预测输出是什么(手动)。然而,我不知道从哪里开始,比如你如何知道哪个变量与什么相关联,以及是否有一定的顺序要遵循(即首先遵循类中的哪个方法?)。您可以引导我完成它们吗?
public class Q3_Array_3E {
public Q3_Array_3E() {
int[][] a = create(6);
int i = a.length - 1;
for (int j = 1; j <= i; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
int[][] create(int n) {
int[][] a = new int[n][];
a[1] = new int[3];
a[1][1] = 1;
for (int i = 2; i < a.length; i++) {
a[i] = new int[i + 2];
for (int j = 1; j <= i; j++) {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
return a;
}
}
// Driver (below)
public class Driver3E {
public static void main(String[] args) {
new Q3_Array_3E();
}
}
解应该是 1 4 6 4 1
最佳答案
程序的入口点:
public static void main(String[] args) { ... }
发生的第一件事 - 创建 Q3_Array_3E
类的实例:
new Q3_Array_3E();
当你调用new
时,你调用类构造函数:
public Q3_Array_3E() {
int[][] a = create(6);
...
}
构造函数的第一行调用create
方法:
int[][] create(int n) {
...
}
你的程序的作用:
1) 创建二维数组并用数字填充它
null
0 1 0
0 1 1 0
0 1 2 1 0
0 1 3 3 1 0
0 1 4 6 4 1 0
2) 遍历该数组的最后一个元素并打印从 1 到 5 的元素
0 1 4 6 4 1 0 -> 1 4 6 4 1
关于java - 如何解决类/驱动程序并知道输出是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59183955/