我在練習Java時遇到了一個問題。通過調用相應的方法創建一個新對象並將其添加到另一個對象中
我有一個類書存儲以下信息:
ID(INT),作者和標題
,我有另一個類書架其中存儲的集合使用Vector/ArrayList的書籍並具有以下方法:
addBook :將書籍對象作爲輸入,將對象添加到書架中,方法不返回任何內容。
returnListOfBooks:不帶參數並返回按字母順序按標題排序的所有書籍的Vector/ArrayList。
returnListOfBooksByAuthor:發生在筆者作爲輸入,並通過筆者
,我停留在現在的問題返回書矢量/ ArrayList的是:
主程序,BookShelfApp,執行以下操作:
•創建Book對象的ArrayList。
•創建BookShelf對象。
•每次一本書放在書架:
○程序必須索要書(一)ID的用戶,(B)一書的作者,以及(c)標題這本書。
o創建一個新的Book對象。
o通過調用相應的方法將其添加到BookShelf對象中。
這是我迄今在主要的java上所做的。
public class BookShelfApp {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
ArrayList <Book> myList = new ArrayList<Book>();
BookShelf myBookshelf = new BookShelf(myList);
System.out.println("Enter ID of the book: ");
int id = sc.nextInt(); sc.nextLine();
System.out.println("Enter the author of the book: ");
String author = sc.nextLine();
System.out.println("Enter title of the book: ");
String title = sc.nextLine();
Book myBook = new Book(); //there is an error
}
我是新來的java所以我不太擅長它。任何幫助將不勝感激!
感謝您的回覆。我編輯了我的問題,並添加了迄今爲止創建的類java,這樣我的問題(希望)會更容易理解。 – Minchae