私のOdyseeのJavaの学習の一環として、クラスBookとクラスライブラリを実装する必要があるタスクがあります。 1つの図書館に最大10冊の図書を保管し、図書館の書籍を検索することもできます。今私の問題は、私の検索方法がうまくいかないことです。ここに誰かが考えていると思いますが、これは自分のコードです:基本的なJavaライブラリ、検索方法は機能しません
public class Library {
int Capacity = 10;
int Volume = 0;
Book[] storage = new Book[10];
public Library() {
System.out.println("Hello, I am a library, which can store up to 10 books!");
this.storage = new Book[10];
}
public void add(Book book) {
if (Volume < Capacity) {
this.storage[Volume] = book;
System.out.println("I added the book " + book + ".");
Volume++;
} else if (Volume >= Capacity) System.out.println("The library is full!");
}
public Book search(String title) {
String result = new String();
for (int i = 0; i < this.Volume; i++) {
if (title.equals(this.storage[i].toString())) {
System.out.println("The book with the title " + title + " exists in the library!");
result = this.storage[i].toString();
} else {
System.out.println("The book with the title " + title + " does not exist in the library!");
return null;
}
}
Book retBook = new Book(result);
return retBook;
}
}
public class Book {
String title;
public Book(String title){
this.title = title;
System.out.println("Book " + title + " created.");}
public String toString(){
return this.title;
};
}
ありがとうございました!
読み取りを。変数は小文字で始まるはずです – Jens
'result = this.storage [i] .toString();'の行の後に戻るか、この行を 'this.storage [i] .toString();' – Jens
なぜ新しい本を作るのですか?あなたは書籍の配列を検索しています - 一致するものを返します。 – duffymo