简单的问题
我想输出一个由*组成的平行四边形,以下是我的代码,编写时候都没有报出错误,可是运行就出错了,我刚学JAVA,不知道错在哪里 ,望专家指点 :public class ping {public static void main(String args[])
{ char p[][]={{'*','*','*','*'},{'\0','*','\0','\0','*'}
,{'\0','\0','*','\0','\0','*'},
{'\0','\0','\0','*','*','*','*'}};
int j=0,i=0;
for(;i<4;i++)
for(;j<7;j++)
System.out.print(p[i][j]);
System.out.println("\n");
}
}
****
* *
* *
**** 这是我想得到图形的大致模样;
----------------解决方案--------------------------------------------------------
数组越界了
----------------解决方案--------------------------------------------------------
程序代码:
public class ping {
public static void main(String args[])
{
char p[][]={
{'*','*','*','*'},
{'\0','*','\0','\0','*'},
{'\0','\0','*','\0','\0','*'},
{'\0','\0','\0','*','*','*','*'}
};
;
for(int i=0 ;i<p.length;i++){
for(int j=0 ;j<p[i].length;j++)
System.out.print(p[i][j]);
System.out.println();
}
}
}
public static void main(String args[])
{
char p[][]={
{'*','*','*','*'},
{'\0','*','\0','\0','*'},
{'\0','\0','*','\0','\0','*'},
{'\0','\0','\0','*','*','*','*'}
};
;
for(int i=0 ;i<p.length;i++){
for(int j=0 ;j<p[i].length;j++)
System.out.print(p[i][j]);
System.out.println();
}
}
}
----------------解决方案--------------------------------------------------------
回复 2楼 lampeter123
麻烦 解释的详细点 好么!!我刚学 不是太了解! 还有p.length是什么意义 ----------------解决方案--------------------------------------------------------
以下是引用wdwy277在2010-12-14 17:47:59的发言:
麻烦 解释的详细点 好么!!我刚学 不是太了解! 还有p.length是什么意义
p.length//数组的大小, 用这个可以避免数组越界 麻烦 解释的详细点 好么!!我刚学 不是太了解! 还有p.length是什么意义
----------------解决方案--------------------------------------------------------