import java.util.Scanner; public class Main { public static void main(String[] args) { int num = 30; int a[]={1, 8, 12, 15, 16, 21, 30, 35, 39}; String s = ""; if(find_num(a,num)==-1)System.out.println("没找到"); else System.out.println(num+"的位置在第"+find_num(a,num)+"个位置"); } public static int find_num(int a[] , int num) { int star = 0; int end = a.length - 1; while(star<=end) { //千万不能star != end 不然少了 star == end 这种可能 int mid = (star+end)/2; if(a[mid]==num)return mid; //找到了 if(a[mid]<num) star = mid+1; if(a[mid]> num)end = mid-1; } return -1; //找不到 } }