Java 试图从一个数组中随机选择问题,答案在一个单独的数组中?
我正在尝试制作一个程序,从数组中随机询问您一些问题。你回答他们,它会告诉你是否正确 我有一个问题:Java 试图从一个数组中随机选择问题,答案在一个单独的数组中?,java,arrays,Java,Arrays,我正在尝试制作一个程序,从数组中随机询问您一些问题。你回答他们,它会告诉你是否正确 我有一个问题: String[] questions = {"Type A", "Type B", "Type C", "Type D", "Type E", "Type F", "Type G"}; 然后我有一个答案数组。它们相互对应,因此答案[0]是问题[0] String[] answerKey = {"A", "B", "C", "D", "E", "F", "G"}; int questionNu
String[] questions = {"Type A", "Type B", "Type C", "Type D", "Type E", "Type F", "Type G"};
然后我有一个答案数组。它们相互对应,因此答案[0]
是问题[0]
String[] answerKey = {"A", "B", "C", "D", "E", "F", "G"};
int questionNum = 0; //Used as a counter for the questions.
这是我的代码尝试
public void setUpQuestion() {
String[] answers = new String[4]; //An array of possible answers.
ArrayList<Integer> usedNumbers = new ArrayList<Integer>(); //An Arraylist of question positions.
ArrayList<Integer> usedQuestions = new ArrayList<Integer>(); //A copy of the usednumbers arraylist
for (int i = 0; i < answerKey.length; i++) //Add 0 through 6 to this arraylist, represents the questions
usedNumbers.add(i);
Collections.shuffle(usedNumbers); // Shuffle the arrayList into a random order
usedQuestions = usedNumbers; //Copy the array
for (int a = 0; a < answers.length; a++) { // This for loop should add random answers to the answers array
if (a == 0) {//For the first time around, add the correct answer into the array
answers[a] = answerKey[usedQuestions.get(questionNum)]; //answers[0] = the correct answer to the question
usedNumbers.remove(questionNum); //remove it from the copied array, so we don't accidentally use it twice
} else if (a != 0) { //For the rest of the spots, randomly pick answers that are wrong
answers[a] = answerKey[usedNumbers.get(0)];//pick a random answer that is next
usedNumbers.remove(0);//Remove it so it isnt used again
}
}
lblQuestion.setText(questions[usedQuestions.get(questionNum)]); // Set the label to the question
shuffleArray(answers);//shuffle the array of answers
btnA.setText(answers[0]); //set the button to one of the answers, etc;
btnB.setText(answers[1]);
btnC.setText(answers[2]);
btnD.setText(answers[3]);
}
public void setUpQuestion(){
String[]answers=新字符串[4];//可能的答案数组。
ArrayList UsedNumber=new ArrayList();//问题位置的ArrayList。
ArrayList usedQuestions=new ArrayList();//UsedNumber ArrayList的副本
for(int i=0;i
希望我已经对它进行了充分的注释,以便您知道代码在做什么
这在理论上似乎应该是可行的,但是,当我运行它时,会发生以下情况:
没有列出正确的答案
如果有人能帮我弄清楚为什么这不起作用,我会非常感激
谢谢大家!
编辑: 我尝试了另一种可能更有效的方法。我创建了一个问题对象,它的参数是stringquestion,stringanswer 所以,我做了一堆新的提问对象
public int questionNum = 0; //this is a global variable
Question a = new Question("Type A", "A");
Question b = new Question("Type B", "B");
Question c = new Question("Type C", "C");
Question d = new Question("Type D", "D");
Question e = new Question("Type E", "E");
Question[] questionBank = {a, b, c, d, e};
ArrayList<Question> questionArray = new ArrayList<Question>();
public int questionNum=0//这是一个全局变量
问题a=新问题(“类型a”、“a”);
问题b=新问题(“b类”、“b类”);
问题c=新问题(“c类”、“c类”);
问题d=新问题(“类型d”、“d”);
问题e=新问题(“类型e”、“e”);
问题[]问题库={a,b,c,d,e};
ArrayList questionArray=新的ArrayList();
下面是我对代码的新尝试:
for (int i = 0; i < questionBank.length; i++) questionArray.add(questionBank[i]);//Add all question objects to an arraylist
String[] answers = new String[4];//possible answers
Collections.shuffle(questionArray);//randomize thequestion array,
for (int a = 0; a < answers.length; a++) {
if (a == 0) {//First time around, add the correct answer to the array
answers[a] = questionArray.get(questionNum).getA();//getA() method gets the answer from the question array
} else if (a != 0) {
answers[a] = questionArray.get(a).getA();
questionArray.remove(a);
}
}
lblQuestion.setText(questionArray.get(questionNum).getQ()); //Gets the current question
btnA.setText(answers[0]);
btnB.setText(answers[1]);
btnC.setText(answers[2]);
btnD.setText(answers[3]); //set the answers on the buttons
questionArray.remove(questionNum);
for(inti=0;i
更改
usedQuestions = usedNumbers;
到
输出是什么?正如你所想,它是3
h
得到了3的值j
获得了与h
相同的值h
已更改j
未更改j
是h
的上一个值ArrayList<Integer> usedNumbers = new ArrayList<Integer>();
ArrayList<Integer> usedQuestions = new ArrayList<Integer>();
for (int i = 0; i < 3; i++) // add 0, 1, 2
usedNumbers.add(i);
usedQuestions = usedNumbers;
usedNumbers.remove(0);
System.out.println(usedQuestions);
可能只是
ArrayList<Integer> usedQuestions;
shufflearlay(答案)
-想解释一下你在这里做什么吗?为什么首先要将问题和答案分开成不同的数组?为什么不做一个列表
,其中QA
包含问题和答案?JDK中存在Collections.shuffle()?封装会建议您将它们放在某个新类的对象中。@SpencerCarlson如果您随机排列答案,您如何期望答案的顺序仍然与问题的顺序相同???@fge如果您可以随机选择一个,为什么要排列?它现在显示正确的答案!为什么会这样呢?从测试来看,它会显示三到四次相同的问题。(我做了个A-Z)你知道为什么会发生这种事吗?@SpencerCarlson!如果您自己无法解决问题,请尝试编辑您的问题。但是,使用新类的第二种方法更好。
ArrayList<Integer> usedNumbers = new ArrayList<Integer>();
ArrayList<Integer> usedQuestions = new ArrayList<Integer>();
for (int i = 0; i < 3; i++) // add 0, 1, 2
usedNumbers.add(i);
usedQuestions = usedNumbers;
usedNumbers.remove(0);
System.out.println(usedQuestions);
ArrayList<Integer> usedQuestions = new ArrayList<Integer>();
ArrayList<Integer> usedQuestions;
ArrayList<Integer> usedQuestions = new ArrayList<Integer>();
ArrayList<Integer> usedNumbers = new ArrayList<Integer>();