java 不知道哪里出问题了 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
classXY{publicstaticvoidmain(String[]args){int[]p={8,21,5,54,2};System.out.println(a(...
class XY
{
public static void main(String[] args)
{
int[] p={8,21,5,54,2};
System .out.println(a(p));
}
public static int a(int[] p)
{
int mix=8;
for (int x=0; x<p.length; x++ )
{
for (int y=0;y<=p.length ;y++ )
{
if (p[x]<p[y])
mix=p[x];
else
mix=p[y];
}
}
return mix;
}
}
报错时这样的
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at XY.a(XY.java:16)
at XY.main(XY.java:7) 展开
{
public static void main(String[] args)
{
int[] p={8,21,5,54,2};
System .out.println(a(p));
}
public static int a(int[] p)
{
int mix=8;
for (int x=0; x<p.length; x++ )
{
for (int y=0;y<=p.length ;y++ )
{
if (p[x]<p[y])
mix=p[x];
else
mix=p[y];
}
}
return mix;
}
}
报错时这样的
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at XY.a(XY.java:16)
at XY.main(XY.java:7) 展开
展开全部
数组越界
for (int y=0;y<=p.length ;y++ )
应该是
for (int y=0;y<p.length ;y++ )
for (int y=0;y<=p.length ;y++ )
应该是
for (int y=0;y<p.length ;y++ )
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询