这是我第一次使用二进制搜索,所以我遇到一个小问题(希望!)首先是程序,它允许用户输入一个随机数,如果该数字匹配任何一本书它输出标题。
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);
}
那么我缺少什么让这项工作?有什么想法吗?
答案 0 :(得分:1)
您的getBook
函数声明为:
public static Book getBook(Book [] A, int left, int right, String bookTitle) {
当您尝试仅使用两个String参数调用它时:
Book b = getBook(book1, book2);
如果要调用函数,则必须使用预期的参数调用它。
此外,不确定是否相关,但在构造函数的末尾缺少}
。
顺便说一句,添加你得到的错误将有助于我们帮助你。