2013-05-19 88 views
-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); 
    } 

所以我失去了什麼,使這項工作?有任何想法嗎?

回答

1

getBook函數聲明爲:

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

當您嘗試只用兩個字符串參數來調用它:

Book b = getBook(book1, book2); 

如果要調用一個函數,你必須把它預期的論點。

此外,不確定是否相關,但你在構造函數的末尾缺少}

順便說一句,添加錯誤你會得到幫助我們幫助你。