Example3_8
2013-03-31 10:52 郭莹莹1114010807 阅读(108) 评论(0) 编辑 收藏 举报import java.util.Scanner;
public class Example3_8{
public static void main(String args[]){
int n,start,end,middle;
int a[]={-2,1,4,5,8,12,17,23,45,56,90,100};
start=0;
end=a.length;
middle=(start+end)/2;
int count=0;
Scanner reader=new Scanner(System.in);
System.out.print("请输入一个整数:");
n=reader.nextInt();
while(n!=a[middle]){
if(n>a[middle]){
start=middle;
}
else if(n<a[middle]){
end=middle;
}
middle=(start+end)/2;
count++;
if(count>a.length/2)
break;
}
if(count>a.length/2)
System.out.println(n+"不在数组中");
else
System.out.println(n+"是数组中"+middle+"个元素");
}
}