2015-02-23 89 views
1

我遇到了返回已創建的對象書的問題,任何幫助將非常感激。該程序要求一本書,然後是作者,並將其存儲在一個對象(書)中。然後它被保存到一系列書籍中。Java |找不到符號|返回對象

**import java.util.*; 
public class BookShop { 
    public static void main(String[] args) { 
     Scanner kybd = new Scanner(System.in); 
     book[] books = new book[10]; 

     for (int i = 0; i < books.length; i++){ 
      books[i] = getBook(kybd); 
     } 

     printBookDetails(books); 
    } 

    private static book getBook(Scanner kybd) { 
     System.out.print("What Is The Title Of The Next Book?:> "); 
     String title = kybd.nextLine(); 

     System.out.print("Who Is The Author Of The Next Book?:> "); 
     String author = kybd.nextLine(); 

     if(author == null){ 
      book definedBook = new book(); 
      definedBook.setTitle(title); 
     } 
     else{ 
      book definedBook = new book(); 
      definedBook.setTitle(title); 
      definedBook.setAuthor(author); 
     } 

     return definedBook; 

    } 

    private static void printBookDetails(book[] books) { 

    } 
}** 
+0

哪裏是書的重要嗎?它在同一個包裏嗎? – 2015-02-23 05:16:03

+4

移動book definedBook = new book();到if ... else語句的外部。 – 2015-02-23 05:17:08

+0

你在哪裏有問題?你是否得到例外? – jmcg 2015-02-23 05:18:01

回答

3

而是在if/else語句,只是,如果喜歡把它定義之外定義的書對象:

book definedBook = new book(); 

if (..) { 
    //setter 
} else { 
    //setter 
    ... 
} 
return definedBook; 
+0

非常感謝你!這就是爲我排序! – 2015-02-23 05:18:00

1

移動的book的聲明,以便它的範圍,想

book definedBook = new book(); 
if (author == null){ 
    // book definedBook = new book(); 
    definedBook.setTitle(title); 
} else{ 
    // book definedBook = new book(); 
    definedBook.setTitle(title); 
    definedBook.setAuthor(author); 
}