import java.util.Scanner;
public class Library {
public static void main(String [] args) {
/*
* Patron class: name + 3 books
* Book class: author + title
*/
Book TwelveYears = new Book ("12 Years a Slave", "Solomon Northup");
Book Diary = new Book ("The Diary of John Smith", "John Smith");
Book Fahrenheit451 = new Book ("Fahrenheit 451", "Ray Bradbury");
Book Gatsby = new Book ("The Great Gatsby", "Fitzgerald");
Book Antigone = new Book ("12 Years a Slave", "Sophocles");
System.out.println("Welcome to library account management! Here is a list of books to check out: " +TwelveYears.getBookTitle()+", "+Diary.getBookTitle()+", "+Fahrenheit451.getBookTitle()+", "+Gatsby.getBookTitle()+", "+Antigone.getBookTitle()+".");
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name!");
String enteredName = sc.nextLine();
System.out.println("Enter your first book! Enter it exactly as seen above.");
String book1 = sc.nextLine();
System.out.println("Enter your second book!");
String book2 = sc.nextLine();
System.out.println("Enter your third book!");
String book3 = sc.nextLine();
Patron patron1 = new Patron(enteredName, book1, book2, book3);
book1 = //line I need help on
System.out.println("Here are the books you are checking out: " +book1+ ", " +book2+ ", " +book3+ ".");
}
}
在這裏,我試圖把book1變量,並正確地與一個類的成員assosciate它。我認爲解釋我在這裏要做的最好的方法是用一個例子:如果用戶輸入的book1是「12年奴隸」,那麼book1的值將是「TwelveYears」,這是開始附近的類定義之一的代碼,所以在將來,我可以使用像book1.getAuthor這樣的訪問器/增變器方法。使用掃描儀的輸入來分配類
我剛剛學習java,所以我缺乏正確解釋這個最有可能的方式,所以我很抱歉,但任何幫助將不勝感激,並且我會很樂意回答任何問題,如果我正在嘗試在這裏做,但我希望我很清楚。
謝謝!
book1.getAuthor(),因爲BOOK1將無法正常工作是一個實例一個字符串。如果那是你想要的。 – Sekula1991
是否指您如何以結構化的方式構建您的代碼以根據用戶輸入的內容來檢索圖書?比如用戶輸入書名或作者是否返回相關書籍? – whitecoffee