# 谁帮我看看我重写的binarySearchArray，为什么不能正确查找及输出预期结果。希望给出详细解释，IT小菜一枚。

0

``` 1 import java.util.Arrays;
2 import java.util.Scanner;
3
4 public class Array {
5     public static void main(String[] args) {
6         int[] arr = new int[10] ;
7         RandomAccessArray.randomAccessArray(arr,10);//调用自动生成有序数组的方法
8         //输入一个数
9         Scanner scan = new Scanner(System.in);
10         System.out.print("请输入一个0～100以内的整数： ");
11         int search = scan.nextInt();
12         int index = binarySearchArray(arr, search);
13
14     }
15     public static int binarySearchArray(int[] arr ,int search){
16         int startIndex =0,endIndex=arr.length;
17
18         int midIndex = (startIndex+endIndex)/2;
19         int mid = arr[midIndex];
20         while(startIndex <= endIndex){22                 if (search >  mid){
23                     startIndex = midIndex +1;
24                 }else if (search < mid ){
25                     endIndex = midIndex -1;
26                 }else{
27
28          System.out.println("您要找的值"+search+"在数数列的下表为： "+ midIndex);
29                     return midIndex;
30             　　}32             if (startIndex > endIndex){
33                 System.out.println("您要查找的数不在数列中");
34             }
35         }
36         return -1;
37     }
38
39 }
```
IT_xiaocai | 初学一级 | 园豆：199

1

public static int binarySearchArray(int[] arr, int search)
{
int startIndex = 0, endIndex = arr.length;

int midIndex = (startIndex + endIndex) / 2;
int mid = arr[midIndex];
while (startIndex <= endIndex)
{
if (search > mid)
{
startIndex = midIndex + 1;
midIndex = (startIndex + endIndex) / 2;
mid = arr[midIndex];
}
else if (search < mid)
{
endIndex = midIndex - 1;
midIndex = (startIndex + endIndex) / 2;
mid = arr[midIndex];
}
else
{

System.out.println("您要找的值" + search + "在数数列的下表为： " + midIndex);
return midIndex;
}
if (startIndex > endIndex)
{
System.out.println("您要查找的数不在数列中");
}
}
return -1;
}

kingreatwill | 菜鸟二级 |园豆：383 | 2017-02-23 14:37

IT_xiaocai | 园豆：199 (初学一级) | 2017-02-23 22:45

public static int binarySearchArray(int[] arr ,int search){
int startIndex =0,endIndex=arr.length;
while(startIndex <= endIndex){
int midIndex = (startIndex+endIndex)/2;
int mid = arr[midIndex];
if (search > mid){
startIndex = midIndex +1;
}else if (search < mid ){
endIndex = midIndex -1;
}else{
System.out.println("您要找的值"+search+"在数数列的下表为： "+ midIndex);
return midIndex;
}
if (startIndex > endIndex){
System.out.println("您要查找的数不在数列中!");
}
}
return -1;
}

IT_xiaocai | 园豆：199 (初学一级) | 2017-02-23 22:48

1



How to change?

0

public static int binarySearchArray(int[] arr ,int search){
int startIndex =0,endIndex=arr.length;
while(startIndex <= endIndex){
int midIndex = (startIndex+endIndex)/2;
int mid = arr[midIndex];
if (search > mid){
startIndex = midIndex +1;
}else if (search < mid ){
endIndex = midIndex -1;
}else{
System.out.println("您要找的值"+search+"在数数列的下表为： "+ midIndex);
return midIndex;
}
if (startIndex > endIndex){
System.out.println("您要查找的数不在数列中!");
}
}
return -1;
}

IT_xiaocai | 园豆：199 (初学一级) | 2017-02-23 22:51

您需要登录以后才能回答，未注册用户请先注册