2014-12-06 86 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; 
}