【发布时间】:2014-12-30 10:49:16
【问题描述】:
我是初学者。 这是一个二进制搜索代码。它显示主方法的数组越界错误。 请查看该程序并告诉我我的错误。感谢您的服务。 我必须写所有这些废话,因为我不能发布它,因为它要求更多细节。
public class BinaryS
{
int n;
public BinaryS(int z)
{
n=z;
}
static int pos;
static boolean flag=false;
public void disp()
{
int arr[]={0,1,2,3,4};
int len=arr.length;
int first=0;
int last=len;
int mid=(int)(first+last/2);
//boolean flag=false;
while(mid>=0 && mid<=len)
{
if(n<arr[mid])
{
last=mid;
}
if(n>arr[mid])
{
first=mid;
}
if(n==arr[mid])
{
flag=true;
pos=mid+1;
}
}
if(flag==true){
System.out.println("the no."+n+"is found at"+pos);
}
else{
System.out.println("the no."+n+"is not found ");
}
}
public static void main(String args[])
{
BinaryS obj=new BinaryS(2);
obj.disp();
}
}
【问题讨论】:
-
首先要修复:缩进。现在到处都是。正确缩进它,我们所有人都会更容易阅读,包括你自己。接下来,更清楚地描述问题,以及您为尝试诊断它所做的工作。 (你说它没有编译但它一直在运行,这对我来说听起来很矛盾......你需要非常准确地了解你所看到的。)
-
编译错误应该很容易修复。它会告诉你犯错的类名和行号。我的建议是清理编译错误并在遇到运行时问题时返回。
标签: java search compiler-errors binary