高手帮帮我看看这个程序

yuyeyi 2007-11-30 09:18:31
帮我看下这个程序有什么错误,
说是数组越界
class pass{
int i,j,k=0,c,d,size=3;
int a[][]=new int[size][size];
int n=size;
void xunhuan(){
for(j=0,i=0;i<=n;c=i++){
a[c][j]=++k;
System.out.println(+k);
if(i==0&&j==0)
{
for(j=0;j<=n;j++){
a[c][j]=d++;
System.out.print(+d);}
}
else{
for(j=0;j<=n;j++)
{a[c+1][j]=a[c][j-1]+a[c][j];
System.out.print(+a[c+1][j]);}
}
}
}
public static void main(String args[]){
pass b=new pass();
b.xunhuan();
}
}
要求输出结果是:
1 2 3
2 3 5
3 5 8
...全文
146 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
bukebuhao 2007-12-04
  • 打赏
  • 举报
回复
兄弟,代码格式要注意,看看我的方法

class pass {
int i,j,k,c,d,size=5;
int a[][]=new int[size][size];
int n=size;
// void xunhuan() {
// for(j = 0,i = 0; i <= n; c = i++) {
// a[c][j] = ++k;
// System.out.println(k);
// if(i == 0 && j == 0) {
// for(j=0; j <= n; j++) {
// a[c][j] = d++;
// System.out.print(d);
// }
// } else{
// for(j = 0; j <= n; j++) {
// // if j = 0 a[c][j-1] = a[c][-1] break limit
// a[c+1][j] = a[c][j-1]+a[c][j];
// System.out.print(a[c+1][j]);}
// }
// }
// }
void dataSequence() {
//Max
int a1 = 1, a2 = 2;
if (this.size > 0) {
int[] dataArray = new int[size * 2 -1];
dataArray[0] = a1;
dataArray[1] = a2;
for (int i = 2; i < dataArray.length; i++) {
dataArray[i] = dataArray[i-1] + dataArray[i-2];
}
// all data
for (int j = 0; j < dataArray.length; j++) {
System.out.print(dataArray[j] + " ");
}
System.out.println("next line");
// out
for (int k = 0; k < this.size; k++) {
for (int m = k; m < this.size + k; m++) {
System.out.print(dataArray[m] + " ");
}
System.out.println("");
}
}
}
public static void main(String args[]) {
pass b = new pass();
b.dataSequence();
}
}



victoryzll 2007-12-04
  • 打赏
  • 举报
回复
。。。。。。。。。。
乱码就不要贴出来撒
zidane1983 2007-12-04
  • 打赏
  • 举报
回复
真练眼力!
aniude 2007-12-04
  • 打赏
  • 举报
回复
历史告诉我们,写这样的代码要特别小心
cwjieNo5 2007-12-04
  • 打赏
  • 举报
回复
贴代码的时候就不会优化一下
这样看谁看的清楚啊
dsoajjj 2007-12-01
  • 打赏
  • 举报
回复
同意3楼的观点
Ant 2007-12-01
  • 打赏
  • 举报
回复
这位同学,我不知道你的程序目的是什么,但是你注意你的for循环

都是for(j=0;j<=n;j++)或者for(i=0;i<=n;i++)
如果你的n是3,for循环会走4次的,你是3×3的数组,走到第四次不越界才怪呢
wang_yingwei 2007-12-01
  • 打赏
  • 举报
回复
上面的错了,晕死,应改为:
class pass{
int i,j,k=0,c,d,size=3;
int a[][]=new int[size][size];
int n=size;
void xunhuan()
{
for ( i = 0,j=0; i < n; i++ )
{
for ( j = 0; j < n; j ++ )
{
if ( i == 0 && j == 0 )
{
a[i][j] = 1;
}
else
{
if ( (i-1) >= 0 && ( j+1 ) < n )
{
a[i][j] = a[i-1][j+1];
}
else
{
int j1 = (j - 1) > 0 ? (j-1) : 0;
int j2 = (j - 2) > 0 ? (j-2) : 0;
a[i][j]=a[i][j1] + a[i][j2];
}
}
}
}
for ( i = 0; i < n; i++ )
{
for ( j =0; j < n; j++ )
System.out.print(" " + a[i][j]);
System.out.println("");
}
}
public static void main(String args[]){
pass b=new pass();
b.xunhuan();
}
}
wang_yingwei 2007-12-01
  • 打赏
  • 举报
回复
下在是我优化过的代码
class pass1{
int i,j,k=0,c,d,size=3;
int a[][]=new int[size][size];
int n=size;
void xunhuan()
{
for ( i = 0,j=0; i < n; i++ )
{
for ( j = 0; j < n; j ++ )
{
if ( i == 0 && j == 0 )
{
a[i][j] = 1;
}
else
{
if ( (i-1) > 0 && ( j+1 ) < n )
{
a[i][j] = a[i-1][j+1];
}
else
{
int j1 = (j - 1) > 0 ? (j-1) : 0;
int j2 = (j - 2) > 0 ? (j-2) : 0;
a[i][j]=a[i][j1] + a[i][j2];
}
}
}
}
for ( i = 0; i < n; i++ )
{
for ( j =0; j < n; j++ )
System.out.print(" " + a[i][j]);
System.out.println("");
}
}
public static void main(String args[]){
pass b=new pass();
b.xunhuan();
}
}
wang_yingwei 2007-12-01
  • 打赏
  • 举报
回复
把上面的size=4改为size=3;这是我自已试size=4是不是我想要的值。
size=3时,输出
1 2 3
2 3 5
3 5 8
size=4时输出
1 2 3 5
2 3 5 8
3 5 8 13
5 8 13 21
size=5时输出
1 2 3 5 8 13
2 3 5 8 13 21
3 5 8 13 21 34
5 8 13 21 34 55
8 13 21 34 55 89
wang_yingwei 2007-12-01
  • 打赏
  • 举报
回复
class pass{
int i,j,k=0,c,d,size=4;
int a[][]=new int[size][size];
int n=size;
void xunhuan()
{
for ( i = 0,j=0; i < n; i++ )
{
for ( j = 0; j < n; j ++ )
{
if ( i == 0 )
{
if ( j ==0 )
{
a[i][j] = 1;
}
else
{
int j1 = (j - 1) > 0 ? (j-1) : 0;
int j2 = (j - 2) > 0 ? (j-2) : 0;
a[i][j]=a[i][j1] + a[i][j2];
}
}
else //i>0
{
if ( ( j+1 ) < n )
{
a[i][j] = a[i-1][j+1];
}
else
{
int j1 = (j - 1) > 0 ? (j-1) : 0;
int j2 = (j - 2) > 0 ? (j-2) : 0;
a[i][j]=a[i][j1] + a[i][j2];
}
}
}
}
for ( i = 0; i < n; i++ )
{
for ( j =0; j < n; j++ )
System.out.print(" " + a[i][j]);
System.out.println("");
}
}
public static void main(String args[]){
pass b=new pass();
b.xunhuan();
}
}

不知道是不是你要的结果
donald82 2007-11-30
  • 打赏
  • 举报
回复
看错了,SORRY
donald82 2007-11-30
  • 打赏
  • 举报
回复
好像没有定义你变量C的大小吧?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧