2

メソッド searchSong() がメインで機能しません。クラスSongのオブジェクト配列は次のとおりです

public class Library{
 Song[] thelist=new Song[10];
 int counter=0;
 private int i=0;

public void addSong(Song s){//adds Song method to array
    if(i<thelist.length){
    thelist[i]=s;
    i++;}
    else

  public Song searchSong(String title, String album, String author, String interpreter) {   
    for(int j=0;j<thelist.length;j++)
        if(thelist[j].title.equals(title) && thelist[j].album.equals(album) && thelist[j].author.equals(author) &&
             thelist[j].interpreter.equals(interpreter))

                return thelist[j];


            return null;}

私のメインでは、thelist[j] を返すために、文字列のタイトル、アルバム、作成者、通訳者を入力する必要があります。

これが私のマニンです

 Library list=new Library();
 Song one=new Song();
 list.addSong(one);
 one.title="hello";
 one.album="world";
 one.interpreter="luar";
 one.author="me";
}
 list.searchSong(hello,world,luar,me);

list.searchSong() メソッドは何かを返すはずですが、このエラーが発生し続けます

 TestLibrary.java:31: error: cannot find symbol
    list.searchSong(hello,world,luar,me);
                    ^
  symbol:   variable hello
   location: class TestLibrary
    TestLibrary.java:31: error: cannot find symbol
    list.searchSong(hello,world,luar,me);
4

4 に答える 4

5

hello,world,luar,me を二重引用符で囲みます: "hello","world","luar","me"

于 2013-06-28T04:36:52.497 に答える
3

、、、またはという名前の変数はありません。それがJavaが探しているものです。helloworldluarme

あなたのSongオブジェクトの構造 (またはなぜこれを行うのか) はわかりませんが、これはあなたが望んでいるようです。これらのフィールドはStringリテラルであると推測します。そうしないと、はるかに早い段階でコンパイルに失敗します。

list.searchSong(one.title, one.album, one.interpreter, one.author);

または、文字列リテラルを渡すこともできます。ただし、すでにその情報が 1 つのオブジェクトに存在しているため、これは無駄のように思えます。

ああ、戻り値についても何もしません。のインスタンスでそれをキャプチャしたいでしょうSong

Song result = list.searchSong(one.title, one.album, one.interpreter, one.author);
于 2013-06-28T04:37:21.893 に答える
2

そのはず :

list.searchSong("hello","world","luar","me");
于 2013-06-28T04:37:03.843 に答える
0

searchSong() メソッドのパラメーターで String 値を宣言しました。

したがって、メインでメソッドを呼び出すときは、次のようにする必要があります。

list.searchSong("hello","world","luar","me");

于 2013-06-28T04:42:20.977 に答える