2013-10-08 28 views
0

所以,我正在研究一個名爲Notes計數器的程序。在第二個java文件中,我想要的是詢問用戶,他/她想添加多少筆記,然後按順序顯示所有筆記(1.2 .....)Java - 如何將多個JOptioPane字符串存儲在一個數組中?

我不能把多個JOoptionPane.showInpuDialogs成一個陣列 - user2547460 31秒前這條線編輯

for(int i = 0; userEnterADD >i;i++){ 
String add1 = JOptionPane.showInputDialog("Enter your note here!"); 
numberNotes= new String[userEnterADD];} 

該方法的Abobe應該把從JOoptioPane所有用戶的回答到一個單一的陣列。所以後來我可以打印出所有的筆記爲JOOptionPane作爲一個陣列輸入的用戶,

第二個文件viewerN:

所以我要問用戶,「有多少筆記ü要加」?並將該字符串存儲爲int。然後我想要求用戶「輸入你的筆記」的次數與int(你想添加多少筆記?)的次數一樣多。

然後我想將用戶的答案存儲在一個數組中。字符串numberNotes []數組,並在infoView()中打印出來。希望你能理解這個!謝謝

我想將用戶輸入的註釋打印爲一個陣列,我該怎麼做?

感謝 公共無效的InfoView(){

System.out.println("\n\tYour notes:\n"); 
for(int ii = 0; userEnterADD >ii;ii++){ 
     System.out.println(ii+1 + ". " + numberNotes[ii]); 

    //end for 
    } 
    } 



    // end of the program 
} 
+0

請制定您的問題和詳細的問題得到了下面的輸出。什麼不工作? –

+0

對不起,infoView()沒有顯示我詢問用戶的所有筆記,在一個字符串add1, – user2547460

+0

我不能把多個JOoptionPane.showInpuDialogs放入一個數組 – user2547460

回答

0

你需要做下面的改變

userEnterADD = Integer.parseInt(numbAddn); 
numberNotes = new String[userEnterADD]; // Need to initialize your numberNotes array here. 
... 
for (int i = 0; userEnterADD > i; i++) { 
    String add1 = JOptionPane.showInputDialog("Enter your note here!"); 
    numberNotes[i] = add1; // Add the text received from the user to your array 
} 
.. 
// System.out.println(numberNotes[2]); // Commen this line 

你做了什麼會讓你重寫當前陣列與一個新的String數組。和SOP需要進行評論,因爲

  1. 它不會起任何作用
  2. 它會給萬一用戶想要輸入2個或更小的筆記的ArrayIndexOutOfBoundsException

編輯:

作出上述改變後,我在一個行中運行代碼

Heyyyy 
Hi! Welcome to Notes Counter! 
By Marto ©2013 

     Main Menu (hint: just type the number!) 

1 - Start counting 
2 - View/add notes 
3 - Help 
4 - About 
2 tttttt 

You have successfully added!2  notes! 

    Your notes: 

1. test 
2. test1 
+0

感謝,但InfoView中不工作:(( – user2547460

+0

公共無效的InfoView(){ \t \t \t的System.out.println(「\ n \ tYour說明:\ n 「); \t對(INT II = 0; userEnterADD> II;ⅱ++){ \t \t \t的System.out.println(ⅱ+ 1 +」「+ numberNotes [II]); \t。 \t \t \t //結束爲 \t \t} \t} – user2547460

+0

這很好,但請看看infoView(),我如何打印這個數組? – user2547460

0
for(int i = 0; userEnterADD >i;i++){ 
    String add1 = JOptionPane.showInputDialog("Enter your note here!"); 
    numberNotes= new String[userEnterADD]; 
} 

該代碼將始終覆蓋你的陣列。

爲了一個值添加到一個數組,用它作爲這樣的:

for(int i = 0; userEnterADD >i;i++){ 
    String add1 = JOptionPane.showInputDialog("Enter your note here!"); 
    numberNotes[i] = add1; 
} 

旁註:避免在接下來的時間您的帖子這樣的混亂。 75%的代碼沒有接近相關性,您可以將其忽略出來,這會讓每個人都更容易。學習如何自行識別問題很重要,您經常會通過確保您的問題包含所有信息來發現問題。

+0

這很好,但請看看infoView(),我該如何打印這個數組? – user2547460

+0

有什麼不適用於它? –

+0

最後一個方法,infoView();它不打印出陣列!, – user2547460

相關問題