2015-09-03 3 views
-3

Привет У меня есть библиотека классов не закончена, я получаю проблемы компиляции в библиотеке классаошибки компиляции языка Java

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 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 

Я получаю ошибка компиляции в заимствовании() и printAvailableBooks() и returnBook() ... Основной метод не должен изменять ...

Как исправить код следующих методов: loanBook() и printAvai lableBooks() и returnBook()?

большое спасибо :)

+0

Вы прочитали ошибку? – SLaks

+0

Это ужасное название. Пожалуйста, измените его, чтобы отразить актуальные проблемы. –

+0

например, в printAvailableBooks() метод eclipse говорит: firstLibrary не может быть разрешен переменной ... – ramialsaiad

ответ

-1

В borrowBook(), у вас есть if, затем else, затем else if. else всегда должен идти последним:

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 и secondLibrary не существуют в Library, но я предполагаю, что вы не добавили еще.

В printAvailableBooks() у вас есть два заявления return. Второй никогда не будет достигнут. Кроме того, если вы просто пытаетесь распечатать их, используйте System.out.println().

returnBook() также потерпит неудачу, потому что firstLibrary не существует (той же причине, как borrowBook().

Кроме того, я бы не сделать библиотеки продлить книгу. Только подумайте логически, не связывая его с программированием. Библиотека не В библиотеке есть книги, поэтому, возможно, у вас есть переменная в Библиотеке, в которой хранится коллекция книг.

+0

спасибо .. Я изменил места else и else if .. in printAvailableBooks() метод eclipse говорит: firstLibrary не может быть разрешен переменной ... вы знаете, как ее исправить ?? – ramialsaiad

+0

Прежде всего, я предлагаю вам ознакомиться с последним выпуском Java: «Руководство для начинающих». В вашем коде есть много ошибок, которые предполагают, что вы начинающий Java (ничего плохого в этом, мы все начали там!). Многие ваши вопросы можно было решить, пройдя эту книгу. –

+0

спасибо .. вы знаете, как исправить printAvailableBooks() метод? – ramialsaiad

Смежные вопросы