编辑代码

class Main {

    public static boolean findKey(int[] arr, int k) {
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] == k) {
                return true; // 元素找到,返回true
            } else if (arr[mid] < k) {
                right = mid - 1; 
            } else {
                left = mid + 1; 
            }
        }
        return false; // 元素未找到,返回false
    }

    public static void main(String[] args) {
        int[] arr = {85, 63, 52, 34, 25, 17, 6};
        int k = 6;

        boolean result = findKey(arr, k);

        // 输出结果
        if (result) {
            System.out.println("元素 " + k + " 存在于数组中。");
        } else {
            System.out.println("元素 " + k + " 不存在于数组中。");
        }
    }
}