实验四:用一维数组实现杨辉三角

 

源代码:

package yanghui;

public class sanjiao {

public static void main(String[] args) {

int i=1;
int a[] = new int[8];
for(i=0;i<8;i++) {
a[i]=1;
for (int j=i-1;j>0;j--){
a[j]= a[j-1]+a[j];
}


for (int j=0;j<=i;j++)
{
System.out.print(" "+a[j]);
}
System.out.print("\n");
}
}
}

结果:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1

心得:

1.了解了数组在Java中的定义及使用。

2.在运行时出现的错误不能及时更改,缺乏程序调试能力,需要加强调试能力。

 

posted @ 2019-03-26 18:07  雨中南风  阅读(565)  评论(1编辑  收藏  举报