【问题标题】:compilation errors in java language [closed]java语言中的编译错误[关闭]
【发布时间】:2015-09-03 19:54:23
【问题描述】:

您好,我的库类未完成,我在库类中遇到编译问题

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() 中得到编译错误... main 方法不能修改...

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

非常感谢:)

【问题讨论】:

  • 你读错了吗?
  • 这是一个糟糕的标题。请更改它以反映实际问题。
  • 例如在 printAvailableBooks() 方法 eclipse 中说:firstLibrary 无法解析为变量...
  • 好的兄弟,你建议什么标题...和平
  • 反映实际问题的。

标签: java compilation extends


【解决方案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.");
}

firstLibrarysecondLibraryLibrary 中不存在,但我猜你还没有添加它。

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

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

另外,我不会让 Library 扩展 Book。只需从逻辑上考虑它,而不将其与编程联系起来。图书馆不是一本书。图书馆有书籍,所以也许你在 Library 中有一些变量来保存书籍的集合。

【讨论】:

  • 谢谢 .. 我确实更改了 else 和 else if .. 在 printAvailableBooks() 方法中 eclipse 说:firstLibrary 无法解析为变量...你知道如何纠正吗?
  • 首先,我建议您阅读最新版本的 Java:初学者指南。您的代码中有大量错误表明您是 Java 初学者(这没有错,我们都是从那里开始的!)。你的很多问题都可以通过阅读那本书来解决。
  • 谢谢..你知道如何纠正 printAvailableBooks() 方法吗?
  • firstLibrary 无法解析,因为它不是您的 Library 类中的变量。看起来您可能试图在您的 main 方法中引用 firstLibrary 变量,但这是行不通的。
  • 如果你想打印图书馆里的书,你需要维护一个ListBook 对象。 addBook() 应该将Book 添加到列表中,printAvailableBooks() 应该遍历列表并使用System.out.println() 打印书籍。还有更多的实现和重构需要完成,所以我会通读那本书以更好地理解该语言及其工作原理。
猜你喜欢
  • 2012-08-06
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-29
相关资源
最近更新 更多