Pascaline三角形逻辑在java中不起作用

我正在尝试制作一个java程序来打印Pascaline三角形。但它不能正常工作。代码如下:


 int rows=10;

 int[] array=new int[10], temp=new int[10];

 array[0]=1;

 temp[0]=1;

 System.out.println(1);

 for(int i=1;i<rows;i++)

 {

     for(int j=1;j<=i;j++)

     {

         temp[j]=array[j-1]+array[j];

     }

     for(int term:temp)

     {

         System.out.print(term+"\t");

     }

     System.out.println();

     array=temp;

 }

它给出了以下输出:

1 1

1 1

2 3

1 3 5 5

.....

请告知代码有什么问题。


慕仙森
浏览 72回答 1
1回答

凤凰求蛊

帕斯卡林三角形不是阶乘序列一个建议是(警告我不是Java程序员,如果有什么愚蠢/可以很容易地改进,请不要对我粗鲁):public class Pascaline {&nbsp; &nbsp;public static void main(String args[]) {&nbsp; &nbsp; &nbsp;int n = 10, i, j;&nbsp; &nbsp; &nbsp;int [] f = new int[n];&nbsp; &nbsp; &nbsp;f[0] = 1;&nbsp; &nbsp; &nbsp;for (i = 1; i != n; i++)&nbsp; &nbsp; &nbsp; &nbsp;f[i] = f[i - 1] * i;&nbsp; &nbsp; &nbsp;for(i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; for(j = 0; j <= i; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print((f[i] / (f[i - j] * f[j])) + " ");&nbsp; &nbsp; &nbsp; &nbsp;System.out.println();&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}}编译和执行:pi@raspberrypi:/tmp $ javac Pascaline.java&nbsp;pi@raspberrypi:/tmp $ java Pascaline&nbsp;1&nbsp;1 1&nbsp;1 2 1&nbsp;1 3 3 1&nbsp;1 4 6 4 1&nbsp;1 5 10 10 5 1&nbsp;1 6 15 20 15 6 1&nbsp;1 7 21 35 35 21 7 1&nbsp;1 8 28 56 70 56 28 8 1&nbsp;1 9 36 84 126 126 84 36 9 1&nbsp;为了更漂亮一点:public class Pascaline {&nbsp; &nbsp;public static void main(String args[]) {&nbsp; &nbsp; &nbsp;int n = 10, i, j;&nbsp; &nbsp; &nbsp;int [] f = new int[n];&nbsp; &nbsp; &nbsp;f[0] = 1;&nbsp; &nbsp; &nbsp;for (i = 1; i != n; i++)&nbsp; &nbsp; &nbsp; &nbsp;f[i] = f[i - 1] * i;&nbsp; &nbsp; &nbsp;for(i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp;for(j = 0; j < n-i; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print(" ");&nbsp; &nbsp; &nbsp; &nbsp;for(j = 0; j <= i; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print((f[i] / (f[i - j] * f[j])) + " ");&nbsp; &nbsp; &nbsp; &nbsp;System.out.println();&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}}编译和执行:pi@raspberrypi:/tmp $ javac Pascaline.java&nbsp;pi@raspberrypi:/tmp $ java Pascaline&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 2 1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 1 3 3 1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;1 4 6 4 1&nbsp;&nbsp; &nbsp; &nbsp; 1 5 10 10 5 1&nbsp;&nbsp; &nbsp; &nbsp;1 6 15 20 15 6 1&nbsp;&nbsp; &nbsp; 1 7 21 35 35 21 7 1&nbsp;&nbsp; &nbsp;1 8 28 56 70 56 28 8 1&nbsp;&nbsp; 1 9 36 84 126 126 84 36 9 1&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java