- public class firstday
- {
- public static void main(String[] args)
- {
- int [] a = {3,5,7,9,11,23,56,78};
- int d = getIndex(a,9);
- System.out.print(d);
- }
- public static int getIndex(int[] x ,int b)
- {
- int min=0;
- int max = x.length-1;
- int mid = (x.length-1+0)/2;
-
- while(max>=mid)
- {
- mid = (max+min)/2; // 这条代码放这 可以正常运行
- if(b>x[mid])
- min = mid+1;
- else if(b<x[mid])
- max = mid-1;
- else
- return mid;
- //mid = (max+min)/2; / /这条代码写后 程序会死循环
-
- }
- return -1;
复制代码
求解释-- 。。。
|