简体   繁体   English

如何在循环迭代开始时获取新的随机数 Java

[英]How to get a new random number at the start of iteration of a loop Java

I'm fairly new to coding and as a project I'm trying to create a question and answer program for practical use.我对编码还很陌生,作为一个项目,我正在尝试创建一个实际使用的问答程序。 In theory, what this program should do is ask you for how many questions you want then give you a random selection of questions and the corresponding answer to the questions.理论上,这个程序应该做的是询问你想要多少个问题,然后给你随机选择的问题和问题的相应答案。 Through each iteration of the loop it should give you a new question and answer but for some reason it repeatedly gives the same question and answer.通过循环的每次迭代,它应该给你一个新的问题和答案,但由于某种原因,它会重复给出相同的问题和答案。 Any help would be highly appreciated.任何帮助将不胜感激。

Random rand = new Random();
    int int_random = rand.nextInt(6);
    int storage = int_random;

    String[] questionV = {"Question 1", "Question 2", "Question 3", "Question 4", "Question 5"};
    String[] answerV = {"Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5",};

   public void question_answerV() {

        System.out.println("How many questions would you like? (1-5) ");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        switch (choice) {
            case 1:
                System.out.println(questionV[int_random]);
                System.out.println("Please press any number for the answer");
                Scanner scanner1 = new Scanner(System.in);
                int choiceA = scanner1.nextInt();
                System.out.println(answerV[storage]);
                break;

            case 2:
                for (int i = 0; i < 2 ; i++ ) {
                    System.out.println(questionV[int_random]);
                    System.out.println("Please press any number for the answer");
                    scanner1 = new Scanner(System.in);
                    choiceA = scanner1.nextInt();
                    System.out.println(answerV[storage]);
                }
                break;

            case 3:
                for (int i = 0; i < 3 ; i++ ) {
                    System.out.println(questionV[int_random]);
                    System.out.println("Please press any number for the answer");
                    scanner1 = new Scanner(System.in);
                    choiceA = scanner1.nextInt();
                    System.out.println(answerV[storage]);
                }
                break;

            case 4:
                for (int i = 0; i < 4 ; i++ ) {
                    System.out.println(questionV[int_random]);
                    System.out.println("Please press any number for the answer");
                    scanner1 = new Scanner(System.in);
                    choiceA = scanner1.nextInt();
                    System.out.println(answerV[storage]);
                }
                break;

            case 5:
                for (int i = 0; i < 5 ; i++ ) {
                    System.out.println(questionV[int_random]);
                    System.out.println("Please press any number for the answer");
                    scanner1 = new Scanner(System.in);
                    choiceA = scanner1.nextInt();
                    System.out.println(answerV[storage]);
                }
                break;
        }
    }

Some observations.一些观察。

  • As already mentioned, use only one Scanner instance.如前所述,仅使用一个 Scanner 实例。
  • You are also repeating code.你也在重复代码。 Just use the choice (that you switch on) as the loop terminating value只需使用选择(您打开)作为循环终止值
  • look at the following code:看下面的代码:
    • int_random never changes. int_random永远不会改变。
    • you prompt for choiceA but never use it你提示选择choiceA ,但从不使用它
    • since you initialized storage to int_random at the beginning, you will always get the same q and a .由于您在开始时将storage初始化为int_random ,因此您将始终获得相同的q and a
for (int i = 0; i < 4 ; i++ ) {
  System.out.println(questionV[int_random]);
  System.out.println("Please press any number for the answer");
  scanner1 = new Scanner(System.in);
  choiceA = scanner1.nextInt();
  System.out.println(answerV[storage]);
}

If (and it's not clear) you want to select questions and answers randomly.如果(不清楚)你想随机回答 select 问题和答案。

  • initialize an ArrayList from 0 to n choice as appropriate.根据需要将ArrayList从 0 初始化到 n 选项。

  • Call Collections.shuffle(list) to randomize the list.调用Collections.shuffle(list)随机化列表。

  • then just choose a question number from the list starting with 0, 1, 2... using list.get().然后只需使用 list.get() 从列表中选择一个以 0、1、2... 开头的问题编号。 Use the obtained number for the question and answer.将获得的数字用于问题和答案。 None will be repeated.没有一个会被重复。

  • And I recommend a little defensive programming.我推荐一点防御性编程。 Make certain that choice is within the bounds of the array of questions.确保choice在一系列问题的范围内。 If not, let the user know and reprompt.如果没有,请让用户知道并重新提示。 Otherwise you could get an ArrayIndexOutOfBoundsException .否则你可能会得到一个ArrayIndexOutOfBoundsException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用一个for循环,该循环在每次迭代中移1个新位来获取Java中的8位数字吗? - Can I use a for loop that shifts in 1 new bit each iteration to get some 8-bit number in Java? Java如何在每次while循环开始时获取一个随机数? - Java how to get a random number every time a while loop begins? 如何在Java FOR LOOP中获得指定随机数的数量 - How to get the amount of the specified random number in Java FOR LOOP 计数 Java 中的循环迭代次数 - count number of loop iteration in Java 如果在循环迭代期间输入无效输入,如何让循环在当前迭代中重新开始 - If an invalid input is entered during an iteration of a loop, how do I get the loop to start again at its current iteration "java:如何在循环开始时获取新的数组元素作为循环结束时的结果数组" - java : how to Get new array elements in start of loop as result array at end of loop 如何从JAVA中的for循环的最后一次迭代中获取字符串? - How to get a string from the last iteration from for-loop in JAVA? Java while循环随机数 - Java while loop random number 如何在java循环中跳过2次迭代 - How to skip 2 iteration in java loop 如何在for循环的每次迭代中启动一个操作线程? - How to start a thread for an operation at each iteration of for loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM