2014-12-06 84 views
1

我正在使用Java解決此問題。有誰知道如何從3個問題字符串數組中隨機抽取2個問題?可以說我有一個3x5的字符串數組是這樣的:來自字符串數組的隨機生成器

String TestBank[][] = {{"What color is grass?","A. Green","B. Red","C. Pink","A"}, 
{"Whats the first month called?","A. December","B. January","C. March","B"}, 
{"What shape is a soccer ball?","A. square","B. flat","C. round","C"}}; 

第一列是一個問題,第二個,第4列是答案的選擇,第5列是正確答案。我試圖弄清楚如何隨機從這3個問題中獲得2個問題,並將這2個問題存儲到2行的一維數組中,其中我們使用JOptionPane進行輸出,其中從一維數組中獲取這些問題,在不同的窗口中逐個顯示每個問題,包括答案選項。在回答2個問題後,它根據他/她錯過了多少個問題來告訴用戶得分。

我對Java比較陌生,如果有人能幫助我,我將不勝感激。

+0

你可以使用'Random''s'nextInt(int n)'。看看api [鏈接](http://docs.oracle.com/javase/7/docs/api/java/util/Random.html) – Multithreader 2014-12-06 00:32:15

+0

我看了你發給我的鏈接,但是看起來好像很混亂我。我對Java真的很陌生,所以如果你知道我的意思,我就不是真正的代碼效率。如果你或任何人都可以根據我放下的信息創建一個示例,並向我解釋這將是非常有用的。我基本上是一個視覺學習者。 – Blue 2014-12-06 00:41:53

+0

你好藍,歡迎來到SO。你通常希望在你的問題上更精確一些。問題的哪一部分有問題(選擇隨機問題,如何將這些問題放入數組中,如何使用JOptionPane,如何在窗口中顯示該問題,如何告知用戶分數,其他內容)?你已經嘗試了什麼?爲什麼這不起作用?爲什麼一方的「相關問題」對你沒有幫助? – fishinear 2014-12-06 01:00:45

回答

1

這是你如何在你的情況下使用隨機類。選擇一個隨機整數,其中隨機選擇的最高數字是您的總問題數量。

Random random = new Random(); 
int randomQuestion = random.nextInt(nrOfQuestions); 

,並使用這個變量randomQuestion訪問您從矩陣問題: 題庫[randomQuestion] [0]

乾杯!

+0

那麼如何將數字分配給我的問題呢? – Blue 2014-12-06 00:45:59

+0

PS:在Java標準中,變量名以小寫字母開頭。 – aurelius 2014-12-06 00:46:34

+0

好吧,你有一個矩陣,數組數組,你說你將問題存儲在每一行的第一列。所以問題的總數實際上是你在矩陣中的總行數。 – aurelius 2014-12-06 00:48:42

0

改序,然後縮短陣列可能是要走的路。然而,這在列表中更容易完成。一般來說,集合類應該優先於數組。

這個答案中的大部分工作是將數組轉換爲列表並返回。

private static String[][] randomize(String[][] testBank, int questions) { 
    // convert top array to list of mutable size 
    List<String[]> testBankAsList = new ArrayList<>(); 
    for (String[] qoa: testBank) { 
     testBankAsList.add(qoa); 
    } 

    // randomize questions 
    Collections.shuffle(testBankAsList, new SecureRandom()); 

    // remove the tail 
    testBankAsList.subList(questions, testBankAsList.size()).clear(); 

    // convert back into array 
    String[][] shorterRandomTestBank = testBankAsList.toArray(new String[testBankAsList.size()][]); 

    return shorterRandomTestBank; 
} 

其中questions應該設置爲2當然。我遺漏了所有的參數檢查。

請注意,Arrays.toList不能使用,因爲它只是在後備陣列上創建一個列表,因此clear()操作將失敗。

用法示例:

public static void main(String[] args) { 
    String testBank[][] = {{"What color is grass?","A. Green","B. Red","C. Pink","A"}, 
      {"Whats the first month called?","A. December","B. January","C. March","B"}, 
      {"What shape is a soccer ball?","A. square","B. flat","C. round","C"}}; 
    int questions = 2; 

    String[][] shorterRandomTestBank = randomize(testBank, questions); 

    // iterate of the returned questions 
    for (String[] qoa : shorterRandomTestBank) { 
     // prints the question 
     System.out.println(qoa[0]); 

     // prints the answer 
     System.out.println(qoa[qoa.length - 1]); 
    } 
} 

對不起,我讓你做的Swing/GUI編程自己。如果您遇到問題,請單獨提問。

0

這是一個答案,它處理隨機生成器兩次生成相同問題的可能性。使用下面的代碼,您將只能得到1個問題。此外,我還評論了每一步,讓您瞭解邏輯。如果你有一個int類型的一維數組,那麼你會在檢查過程中遇到問題。這是因爲1D類型的int數組將默認值設置爲0.使用Integer或ArrayList消除了這個問題,因爲默認值爲null而不是0.

我還包括了隨機顯示所需的循環和JOptionPane代碼選擇的問題。由你決定你想要做什麼與答覆。

public static void main(String args[]) { 

    // We are setting this final variable because we don't want to hard code numbers into loops etc 
    final int NUMBER_OF_QUESTIONS_TO_TAKE = 2; 

    String testBank[][] = {{"What color is grass?", "A. Green", "B. Red", "C. Pink", "A"}, 
      {"Whats the first month called?", "A. December", "B. January", "C. March", "B"}, 
      {"What shape is a soccer ball?", "A. square", "B. flat", "C. round", "C"}}; 

    // Initialise the array of questions that have been randomly chosen. 
    String finalArrayOfQuestions[] = new String[NUMBER_OF_QUESTIONS_TO_TAKE]; 

    // ArrayList to store the index number of a question so we can check later if it has been already used by number generator 
    ArrayList<Integer> alreadyChosenList = new ArrayList<Integer>(); 

    // boolean that we will use for whether or not a question has already been selected 
    boolean alreadyChosen; 

    // The column number that the random number generator generates, which is then used to extract the String question 
    int rowToUse; 

    // A for loop is used to loop through the process, depending on how many questions you want to take. 
    for (int i = 0; i < NUMBER_OF_QUESTIONS_TO_TAKE; i++) { 

     // Generate a random number, repeat the process (do/while) until a random number has been generated that hasnt been generated before 
     do { 
      // Generate a random number within the range 
      Random random = new Random(); 
      rowToUse = random.nextInt(testBank.length); 

      //check not already been picked 
      alreadyChosen = alreadyChosen(rowToUse, alreadyChosenList); 
     } while (alreadyChosen); 


     // Get String representation of question chosen at random 
     String questionChosen = testBank[rowToUse][0]; 

     // Add this String to finalListOfQuestions 
     finalArrayOfQuestions[i] = questionChosen; 

     // adds to list of questions already chosen. Makes sure you don't take same question twice. 
     //alreadyChosenList[i] = alreadyChosenList[rowToUse]; 
     alreadyChosenList.add(rowToUse); 
    } 

    for (String questions : finalArrayOfQuestions) { 
     String response = JOptionPane.showInputDialog(questions); 

     /* 
     The response is the answer that the user types in. Here you can check it against the arrays you have 
     Or if you dont want the user to input a response use: 
     JOptionPane.showMessageDialog(null, questions); 
     */ 

    } 
} 


/* 
Method takes row index to use and the ArrayList of row indexes already been used and returns true or false depending if the current one is in the arraylist 
*/ 
private static boolean alreadyChosen(int rowToUse, ArrayList<Integer> alreadyChosenList) { 

    for (int indexToCheck : alreadyChosenList) { 
     if (indexToCheck == rowToUse) { 
      return true; 
     } 
    } 
    return false; 
}