高手帮帮我看看这个程序

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
...全文
155 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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的大小吧?
/* *游戏说明:此为我边学边做的,但有很多的BUG,参照了我从网上下载的游戏方块设计 * 一、游戏的BUG * 1、提示分数那地方,玩过游戏后,长了分数后,再玩时,后面的数据没清掉 * 2、重级BUG:当在游戏过程中,切换了一个旁边的后,再切换回来后, * 刚才的方块不显了,而且游戏区的数据也不见了。 * 3、我在刚开始做时,没有要窗口上的关闭,现在想要,但不知道怎么加。 * 4、如果用鼠标点了菜单后,再回到游戏区,数据方块又不见了。 * * 二、此游戏没有版权,可以乱改,反正我也是在学习,谢谢那些无私的网友们,不过 * 请你们下次提交上来的源程序有个说明好不好,看得我头都大了,流程图也没得, * 设计说明也没有,完全看源代码,很费力的!谢谢对新人的支持。 * * 三、如果那位高手愿意,请收我为徒弟吧,我学过c/c++、数据结构、编译原理、操作系统等( * 计算机专业的),但对于VC这个大东西来说,,好像一点用也没有,现在在边学边做,门不好入呀! * * 四、请高手帮帮我,请给分析一下问题在哪,谢谢,我对VC还不是很清楚,在文件目录下 * 有设计时的流程图。设计说明我没有写,我是针对每一个流程图模块来设计和编码的。 * * 五、在游戏中,我加了很多注释,以方便理解,主要的代码都在CChileView.h、CChileView.CPP中 * 我想的是,如果新人也想看看的话,可能理解起来快点。不过有点乱,编码中有些冗余。没来 * 得及改。如果你改好了,请给我一份,我想学学。谢谢! * *================================================================================================ * *编译环境: * 1、操作系统 :WindowsXP SP2 * 2、编译器 :Visual C++ 8.0 * *包含文件:所有源文件都在此。 * * *编译参考:此目录下有一个文件名为:Russia.sln的文件,用Visual C++ 8.0 打开,直接就可编译 * 此游戏是我编译通过后,才压缩的。如果编译不了,请联系我,下面有我的QQ和email。 * * *其它事宜:如果还有什么问题我没有提到的,请联系我,愿向你学习。 * *================================================================================================ * *Version :BUG Edtion * *Aauthor :lin_liu60 * *E-mail :lin_liu60@163.com * *QQ :994165 (网名:刘羽峰) * *Date :2006/9/27 * */

62,630

社区成员

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

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