import java.util.*; public class Erfen { public static void main(String[] args) { int ary[] = new int[10]; Random a = new Random(); for (int i = 0; i < ary.length; i++) { ary[i] = a.nextInt(30); } System.out.print("随机的数组 "); for (int shuzu : ary) { System.out.print(" " + shuzu); } System.out.println(); System.out.print("排序后的数组"); for (int i = 0; i < ary.length - 1; i++) { for (int j = 0; j < ary.length - 1 - i; j++) { if (ary[j] > ary[j + 1]) { int c = ary[j]; ary[j] = ary[j + 1]; ary[j + 1] = c; } } if (i == 8) { for (int shuzu : ary) { System.out.print(" " + shuzu); } } } System.out.print(" 输入一个数 "); Scanner shu = new Scanner(System.in); int b = shu.nextInt(); int d = -1; int start = 0; int end = ary.length - 1; while (start <= end) { int middle = (start + end) / 2; if (b == ary[middle]) { d = middle; break; } else if (b < ary[middle]) { end = middle - 1; } else if (b > ary[middle]) { start = middle + 1; } } if (d == -1) { System.out.println("输入的数字不在数组中"); } else { System.out.println("输入的数字在数组中是ary[" + d + "]=" + ary[d]); } } }