-1

这是我第一次使用二进制搜索,所以我遇到了一个小问题(希望如此!)首先是程序,它允许用户输入一个随机数,如果该数字与任何书籍匹配,它就会输出标题。

class b {

    String book1, book2;

    b () {
        book1 = "Wicked Awesome Title";
        book2 = "How to Read a Book";

        public static Book getBook(Book [] A, int left, int right, String bookTitle) {
            int middle;
            Book found = null;

            /**Your average Joe binary search...*/
            while (found == null && left <= right) {
                //If middle item == 0, returns true
                middle = (left + right)/2;
                int compare = A[middle].sameTitle(bookTitle);
                if (compare == 0) {
                    found = A[middle];
                } else {
                    if (compare >0) {
                        right = middle -1;
                    } else {
                        left = middle + 1;
                    }
                }
            }
            return found;
        }

现在这就是问题所在,按下“查找”图书按钮后,

private void findActionPerformed(java.awt.event.ActionEvent evt) {                                           

    String book1 = "Wicked Awesome Title";
    String book2 = "How to Read a Book";;

    Book b = getBook(book1, book2); //this entire line is underlined, 
    if (b != null){
        itsATextField.setText("You've found the book " + b);
    }

那么我缺少什么来完成这项工作?有任何想法吗?

4

1 回答 1

1

您的getBook函数声明为:

public static Book getBook(Book [] A, int left, int right, String bookTitle) {

当您尝试仅使用两个字符串参数调用它时:

Book b = getBook(book1, book2);

如果要调用函数,则必须使用预期的参数调用它。

此外,不确定是否相关,但您}在构造函数的末尾缺少。

顺便说一句,添加您得到的错误将帮助我们帮助您。

于 2013-05-19T14:23:47.017 回答