2015-11-30 13 views
0

我試圖瞭解其中陣列可用於各種方式,我發現在本書的例子,我想修改我的陣列正在取得和我能夠創建數組對象引用變量的對象,但我不能夠在這裏讓用戶輸入值是我的代碼:使用戶通過數組在我的類的變量中輸入輸入字符串。

class Information { 
    String author, title; 
} 

public class Books { 

public static void main(String[] args) throws IOException { 

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("Enter Number of Books: "); 
    int n = Integer.parseInt(bufferedReader.readLine()); 
    Information myBooks[] = new Information[n]; 
    for (int i = 0; i < n; i++) { 
     myBooks[i] = new Information(); 
    } 
    for (int j = 0; j < n; j++) { 
     myBooks[j].author = bufferedReader.readLine(); 
     myBooks[j].title = bufferedReader.readLine(); 
    } 
    for (int k = 0; k < n; k++) { 
     System.out.println("author of " + myBooks[k].title + " is " + myBooks[k].author); 
     System.out.println(); 
    } 

} 

} 

我怎樣才能讓用戶輸入值myBooks [0] .author和myBooks [0] .title僞等。

+1

也許可以考慮使用'Scanner'呢?我發現使用用戶輸入更容易,但這只是我的看法。 http://www.java-made-easy.com/java-scanner.html http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html – Michael

+0

@邁克爾我只是犯了一個愚蠢的錯誤,我糾正它顯然我也試過你的建議,掃描儀和bufferedReader都工作,Thanx! – Alok

回答

0

吉茲我覺得愚蠢:P但我得到了解決我的問題。 代碼正在它應該在上班的路上,我只是想添加兩句話和它的工作。基本上這是把我帶到循環,但我不知道。

 for (int j = 0; j < n; j++) { 
     System.out.println("Enter author's name for book "+ (j+1)); 
     myBooks[j].author = bufferedReader.readLine(); 
     System.out.println("Enter Title for book"+ (j+1)); 
     myBooks[j].title = bufferedReader.readLine(); 

所以現在當我進入循環要求您輸入,所以我可以輸入:P

相關問題