-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);
}
所以我失去了什麼,使這項工作?有任何想法嗎?