[英]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.一些观察。
int_random
never changes. int_random
永远不会改变。choiceA
but never use itchoiceA
,但从不使用它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.