如何修复我的类中的编译错误 [英] How to fix the compilation errors in my class
问题描述
我的图书馆需要更正,在第62,63,64,65行我写了eclipse编辑器的错误评论....类库的主要方法一定不能修改......
图书馆课程的输出应该是这样的:
图书馆时间:
图书馆每天上午9点至下午5点开放。
图书馆地址:
10 Main St.
228 Liberty St.
借用指环王:
你成功借用了指环王
对不起,这本书已经借来了。
对不起,这本书不在我们的目录中。
第一个图书馆的书籍:
达芬奇密码
Le Petit Prince
两个城市的故事
第二个图书馆可用书籍:
目录中没有书籍
返回指环王:
您成功退回了指环王
第一个图书馆提供的图书:
达芬奇密码
Le Petit Prince
两个城市的故事
指环王
图书馆课程如下:
Hi , my Library calss need to be corrected , In line 62, 63, 64, 65 I wrote the error comments from eclipse editor .... The main method of class Library must not modify ...
The output of Library class should be like this :
Library hours:
Libraries are open daily from 9am to 5pm.
Library addresses:
10 Main St.
228 Liberty St.
Borrowing The Lord of the Rings:
You successfully borrowed The Lord of the Rings
Sorry, this book is already borrowed.
Sorry, this book is not in our catalog.
Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
Books available in the second library:
No book in catalog
Returning The Lord of the Rings:
You successfully returned The Lord of the Rings
Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
The Lord of the Rings
The Library class is as below :
import java.util.ArrayList;
public class Library {
// Add the missing implementation to this class
ArrayList <string> list = new ArrayList<string>();
String adress;
public Library(String t){
this.adress=t;
}
public void Book(){
}
public void addBook(String Book){
list.add(Book);
}
public static void printOpeningHours(){
System.out.println("Libraries are open daily from 9am to 5pm.");
}
public void printAddress(){
System.out.println(adress);
}
public void borrowBook(String k){
for (int i=list.size()-1; i>=0; i--){
if (list.get(i).equals(k)){
System.out.println("You successfully borrowed The Lord of the Rings");
list.remove(i);
}else{
System.out.println("Sorry, this book is already borrowed.");
}
}
}
public void printAvailableBooks(){
System.out.println(list);
}
public String returnBook(String r){
for (String b : list){
if (b.equals(r)){
return b;
}
}
System.out.println("The Lord of the Ring");
return r;
}
public static void main(String[] args) {
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
// Add four books to the first library
line 62 firstLibrary.addBook(new Book("The Da Vinci Code")); // ***** - The method addBook(String) in the type Library is not applicable for the arguments (Book)
line 63 firstLibrary.addBook(new Book("Le Petit Prince")); // ***** The method addBook(String) in the type Library is not applicable for the arguments (Book)
line 64 firstLibrary.addBook(new Book("A Tale of Two Cities"));// ***** The method addBook(String) in the type Library is not applicable for the arguments (Book)
line 65 firstLibrary.addBook(new Book("The Lord of the Rings"));// ***** The method addBook(String) in the type Library is not applicable for the arguments (Book)
// Print opening hours and the addresses
System.out.println("Library hours:");
printOpeningHours();
System.out.println();
System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();
System.out.println();
// Try to borrow The Lords of the Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");
System.out.println();
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
System.out.println();
System.out.println("Books available in the second library:");
secondLibrary.printAvailableBooks();
System.out.println();
// Return The Lords of the Rings to the first library
System.out.println("Returning The Lord of the Rings:");
firstLibrary.returnBook("The Lord of the Rings");
System.out.println();
// Print the titles of available from the first library
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
}
}
您是否有任何想法更正代码获得所需的输出?
你认为我在addBook()方法中的代码是否正确?
谢谢
Do you have any idea to correct the code to get the desired output ?
Do you think that my code in addBook() method is correct?
thanks
推荐答案
这很简单:你的方法addBook
接受String
参数,但是你试图传递参数类型预订
。这就是全部。
如何解决?不以错误的方式编写调用,或者使用错误的参数编写方法定义。你选择。
-SA
This is simple: your methodaddBook
acceptsString
argument, but you are trying to pass the argument of the typeBook
. That's all.
How to fix? By not writing a call in a wrong way, or a method definition with wrong arguments. You choose.
—SA
这篇关于如何修复我的类中的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!