2017-07-17 27 views
0

我想弄清楚如何從問題列表中拉出然後使用JTextArea輸出它們。現在我被困在試圖弄清楚如何從列表中隨機選擇然後輸出它。這是我迄今爲止。試圖找出如何從數組中拉出並使用JTextArea顯示

public static void Question() 
    { 
    String[] quest = {"place", "hold", "er"}; 
    int[] questNum = {1, 2, 3}; 


    JTextArea question = new JTextArea(6, 20); 
    question.setText("Question " + questNum + quest); 
    } 
+0

的[如何隨機挑選從數組中的元素]可能的複製(https://stackoverflow.com/questions/8065532/how-to-從數組中隨機選取一個元素) – JDC

+0

爲什麼不去掉'questNum'並在任何需要的地方簡單地使用'quest'的索引加1? – tradeJmark

回答

0

迭代陣列的和顯示在JTextArea中

public static void Question() 
    { 
    String[] quest = {"place", "hold", "er"}; 
    int[] questNum = {1, 2, 3}; 

    String textContent = new String(); 
    for (int i = 0; i < quest.length; i++) { 
    textContent += quest[i] + " "; 
    } 

    for (int i = 0; i < questNum.length; i++) { 
    textContent += questNum[i] + " "; 
    } 

    JTextArea question = new JTextArea(6, 20); 
    question.setText("Question " + textContent); 
    } 
+0

在這種情況下,我寧願使用'StringBuilder'而不是'String'。 – msagala25

+0

在這種情況下,使用StringBuffer比String更優選 –

相關問題