繁体   English   中英

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

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

我对编码还很陌生,作为一个项目,我正在尝试创建一个实际使用的问答程序。 理论上,这个程序应该做的是询问你想要多少个问题,然后给你随机选择的问题和问题的相应答案。 通过循环的每次迭代,它应该给你一个新的问题和答案,但由于某种原因,它会重复给出相同的问题和答案。 任何帮助将不胜感激。

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;
        }
    }

一些观察。

  • 如前所述,仅使用一个 Scanner 实例。
  • 你也在重复代码。 只需使用选择(您打开)作为循环终止值
  • 看下面的代码:
    • int_random永远不会改变。
    • 你提示选择choiceA ,但从不使用它
    • 由于您在开始时将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]);
}

如果(不清楚)你想随机回答 select 问题和答案。

  • 根据需要将ArrayList从 0 初始化到 n 选项。

  • 调用Collections.shuffle(list)随机化列表。

  • 然后只需使用 list.get() 从列表中选择一个以 0、1、2... 开头的问题编号。 将获得的数字用于问题和答案。 没有一个会被重复。

  • 我推荐一点防御性编程。 确保choice在一系列问题的范围内。 如果没有,请让用户知道并重新提示。 否则你可能会得到一个ArrayIndexOutOfBoundsException

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM