java - 二进制搜索静态方法遇到问题无法引用

我面临的问题是了解如何利用binary search在给定的array(电影)中搜索用户输入的标题?我对搜索本身有一种感觉,但不了解如何使用返回值显示标题(无论是否找到)。

接下来的代码包括将我的bubble sort引入binary search。这是我一直看到的一些错误消息。

C:\Users\goofy bastard\Documents\JAVA>javac CharlieBrownP5.java
CharlieBrownP5.java:115: error: non-static method binarySearch(Movie[],String) 
cannot be referenced from a static context   
binarySearch(movies, key) = x;

CharlieBrownP5.java:115: error: unexpected type
 binarySearch(movies, key) = x;

  required: variable
  found:    value
CharlieBrownP5.java:134: error: cannot find symbol        if(key.comparTo(movies[mid].getTitle()) > 0) {
  symbol:   method comparTo(String)
  location: variable key of type String
3 errors

码:
    public static void displayTotals(Movie[] movies) {
    double totalRevenue = 0;
    int totalMovies = Movie.getTotalMovies();
    for(int i = 0; i < movies.length; i++) {
        totalRevenue += movies[i].calcRevenue();
    }
    System.out.print("The total number of movies is " + totalMovies +
        " and their total revenue is ");
    System.out.printf("%8.3f", totalRevenue);
    System.out.print(" million dollars.\n\n");

}

public static void searchForMovie(Movie[] movies) {
    Scanner input = new Scanner(System.in);
    boolean needNextPass = true;
    String key;
    Movie temp;


    System.out.print("Enter the Title of the Movie to Search for: ");
    key = input.nextLine();

    for(int pass = 1; pass < movies.length && needNextPass; pass++)
    {
        needNextPass = false;
        for(int x=0; x< movies.length-pass; x++)
        {
            if(movies[x].getTitle().compareTo(movies[x+1].getTitle()) > 0)
            {
                temp = movies[x];
                movies[x] =  movies[x+1];
                movies[x+1] = temp;

                needNextPass = true;
            }
        }
    }
    int x;
    binarySearch(movies, key) = x;

    if (movies[x].getTitle() == key){
        System.out.printf(movies[x].toString());
    }
    else{
        System.out.print("There is no match item found for movie " +
            "with the title " + key);
    }
}

public static void getMenuChoice4() { 

}
public int binarySearch(Movie[] movies, String key) {
    int low = 0;
    int high = movies.length -1;
    while (high >= low) {
        int mid = (low + high) / 2;
        if(key.comparTo(movies[mid].getTitle()) > 0) {
            high = mid -1;
        }
        else if(key == movies[mid].getTitle()){
            return mid;
        }
        else{
            low = mid + 1;
        }

    }   
    return -low - 1;

感谢您在此工作数小时所提供的任何帮助。
我对Java非常陌生,这是我的Java类的作业的一部分。
请保持简单。

最佳答案

该错误说明了一切-binarySearch不是静态的,您正尝试在静态函数中使用它。只需使其静态:

public static int binarySearch(Movie[] movies, String key) {
...

对于另一个错误,您只需要在以下位置交换函数调用和变量:
x = binarySearch(movies, key);

因为binarySearch(movies, key) = x;的意思是将x分配给从函数返回的值(不是相反),这没有意义,因为一旦该表达式完成执行,返回的值就会消失。

而且您只是键入了compareTo错误(缺少e)。

https://stackoverflow.com/questions/15803295/

相关文章:

java - 引用从对象数组进行打印的方法时找不到符号错误

compiler-errors - ICC无法打开头文件

arrays - 数组表达式语法错误

permissions - 为什么即使许可为777,WHMCS在应用其安全提示后仍显示许可错误?

vba - VB-下标超出范围,错误9

java - 开始Java : Printing out a 5 pointed star usin

c++ - 头文件问题

c++ - 在 ns 2.34 (Jiazi YI ns 2.29) 上添加 mp-olsr 时出现

java - Double不是抽象的,并且不会重写ListInterface中的抽象方法getNex

compilation - Fedora 18 上的内核编译错误(在 Macbook 上的 Virt