java语言中的编译错误

时间:2015-09-03 19:54:23

标签: java compilation extends

您好我的库类未完成,我在库类

中遇到编译问题
import java.util.Scanner;
public class Library {
    // Add the missing implementation to this class

    public static void printOpeningHours(){
        System.out.println("Libraries are open daily from 9am to 5pm.");

    }
    public void printAddress(){
        System.out.println("10 Main St.");
        System.out.println("228 Liberty St.");

    }
    public void borrowBook(){
        Scanner sc= new Scanner(System.in);
        String x=sc.nextLine();
        if (x=firstLibrary){
        System.out.println("You successfully borrowed The Lord of the Rings");
        firstLibrary.remove("The Lord of the Ring");
        }else if{
        System.out.println("Sorry, this book is already borrowed.");
        }else (x=secondLibrary){
        System.out.println("Sorry, this book is not in our catalog.");
        }

    }
    public static void printAvailableBooks(){
        return firstLibrary;
        return secondLibrary;
    }
    public void returnBook(){
        firstLibrary.add("The Lord of the Ring");

    }


    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
        firstLibrary.addBook(new Book("The Da Vinci Code"));
        firstLibrary.addBook(new Book("Le Petit Prince"));
        firstLibrary.addBook(new Book("A Tale of Two Cities"));
        firstLibrary.addBook(new Book("The Lord of the Rings"));

        // 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();
    }
}

Library类的输出应该是这样的:

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

我在borrowBook()和printAvailableBooks()以及returnBook()中遇到编译错误...主方法不能修改...

如何更正以下方法的代码:borrowBook()和printAvailableBooks()以及returnBook()?

非常感谢:)

1 个答案:

答案 0 :(得分:-1)

borrowBook()中,您有if,然后是else,然后是else ifelse应始终排在最后:

if (x=firstLibrary){
    System.out.println("You successfully borrowed The Lord of the Rings");
    firstLibrary.remove("The Lord of the Ring");
} else if (x=secondLibrary){
    System.out.println("Sorry, this book is not in our catalog.");
} else {
    System.out.println("Sorry, this book is already borrowed.");
}
firstLibrary中不存在{p> secondLibraryLibrary,但我猜你还没有添加它。

printAvailableBooks()中,您背靠背有两个return语句。永远不会达到第二个。此外,如果您只是想打印它们,请使用 System.out.println()

returnBook()也会失败,因为firstLibrary不存在(与borrowBook()相同的原因。

另外,我不会让Library扩展Book。只是在逻辑上考虑它而不将其与编程联系起来。图书馆不是一本书。图书馆里有图书,所以你可能在图书馆里有一些包含图书集的变量。