【问题标题】:java random arrayjava随机数组
【发布时间】:2010-12-29 11:00:48
【问题描述】:

我想让问题随机,当我给出答案时比较它们是否正确? 有人可以帮帮我吗?

导入 java.io.BufferedReader; 导入 java.io.IOException; 导入 java.io.InputStreamReader; 导入 java.util.Random;

公共类主{

public static void main(String[] args) { String[] aa = { "question aa" }; String[] bb = { "question bb"}; String[] cc = { "question cc"}; String[] dd = { "question dd"}; String[] e = { "answer to question aa" }; String[] f = { "answer to question bb"}; String[] g = { "answer to question cc"}; String[] h = { "answer to question dd"}; // should be here the random question System.out.print("Enter your answer: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String userAnswer = null; try { userAnswer = br.readLine(); } catch (IOException ioe) { System.out.println("IO error trying to read your answer!"); System.exit(1); } System.out.println("Thanks for the answer, " + userAnswer);

}

【问题讨论】:

    标签: java


    【解决方案1】:
    class QuestionAnswer{
    String q;
    String a;
    //getters/setters and const.
    }
    

    1创建一个 DS,因为您的问题集较小且建议保留在内存中

    List<QuestionAnswer> lst = new ArrayList<QuestionAnswer>();
    lst.add(new QuestionAnswer("Question1","Answer1")); 
    lst.add(new QuestionAnswer("Question2","Answer2")); 
    lst.add(new QuestionAnswer("Question3","Answer3")); 
    

    2生成0到list.size()之间的随机整数

    Random r = new Random();
    int index = r.nextInt(lst.size());
    

    3获取问题并打印并接受用户的回答

    System.out.println(lst.get(index).getQ());
    Scanner in = new Scanner(System.in);
    // Reads a single line from the console 
    String answer = in.nextLine();
    

    4将用户的答案与列表进行比较

    if(answer.equalsIgnoreCase(lst.get(index).getA())){
       System.out.println("You are correct. !!");
    } 
    

    【讨论】:

    • life.java,我明白你的想法。但我并不完全相信3 Fetch question 对 OP 来说足够清楚。他所拥有的只是一个随机数,他应该如何弄清楚如何使用该数字来获取Map 的一个元素,这是他的首要问题......我觉得你应该详细说明一下。我的 2 美分。
    • @st0le 谢谢,我意识到以前没有太大帮助,请现在检查
    • life.java,这样好多了。 +1 :)
    【解决方案2】:
    • 将问题保留在问题数组 question[][] = {aa,bb...};
    • 将答案保存在答案数组arrays[][] = {e,f...};
    • 生成介于 0 和 questions.length 之间的随机数
    • questions[random] 和 answers[random] 应该匹配

      String[] aa = { "question aa" };
      String[] bb = { "question bb" };
      String[] cc = { "question cc" };
      String[] dd = { "question dd" };
      
      
      String[][] questions = { aa, bb, cc, dd };
      
      
      String[] e = { "answer to question aa" };
      String[] f = { "answer to question bb" };
      String[] g = { "answer to question cc" };
      String[] h = { "answer to question dd" };
      
      
      String[][] answers = { e, f, g, h };
      
      
      int min = 0;
      int max = 5;
      int random= 0;
      
      
      random = min + (int) (Math.random() * max);
      
      
      questions[random];
      answers[random];
      

    【讨论】:

    • 这部分是什么意思,对不起,我不明白问题[随机];答案[随机];
    • random 将在 [0,5) 范围内,因此如果 random=0 问题[0] 的答案是 answer[0]。那么您可以匹配提出问题[0],用户给出答案,然后将其与答案[0]匹配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2014-12-29
    • 2019-03-23
    • 2016-04-15
    • 2017-04-17
    相关资源
    最近更新 更多