我的老师为他的帕斯卡三角形问题写了一个测试课程。这里是:
public class TestTriangle {
public static void main(String args[]) {
PascalsTriangle triangle = new PascalsTriangle(7);
if (triangle.getElement(5, 2) == 10)
System.out.println("Triangle has passed one test.");
else
System.out.println("Triangle is wrong.");
triangle.showTriangle();
}
}
这是我的代码:
public class PascalsTriangle {
private int rows;
private int column;
private int position;
/**
* Constructor for objects of class PascalsTriangle
*/
public PascalsTriangle() {
rows = 2;
}
public PascalsTriangle(int n) {
rows = n;
}
public int getElement(int n, int k) {
rows = n;
column = k;
//now the equation
int z;
int y;
int d;
for (z = 1; z <= n; z++) { //z is n! at nominator of equation
int a = z;
z = z * n;
z = a + z;
}
for (y = 1; y <= k; y++) { //y is k! at denominator of equation
int b = y;
y = y * k;
y = b + y;
}
int c = n - k;
for (d = 1; d <= c; d++) { //d is (n-k)! at denominator of equation
int e = d;
d = d * c;
d = e + d;
}
position = z / (y * d);
return position;
}
public showTriangle() { //outputs entire triangle
}
}
我唯一的问题是showTriangle
方法。我不知道如何让它输出整个三角形。如果我拥有的唯一公式是查找特定位置,我将如何打印出整个三角形?