我正在进行欧洲国旗测验,而newQuestion方法是为了在所有4个按钮中自动选择答案,但第一个答案选择始终是答案?
问题是case语句中的参数吗?是在我开始定义每个按钮的时候吗?如何解决这个问题,以便所有四个按钮之间的答案选择不同,而不仅仅是第一个按钮?
任何帮助都将不胜感激。
private void newQuestion(int number) {
iv_flag.setImageResource(list.get(number - 1).getImage());
int correct_answer = r.nextInt(3) ;
int firstButton = number-1;
int secondButton=number;
int thirdButton=number;
int fourthButton=number;
switch (correct_answer) {
case 0:
b_answer1.setText(list.get(firstButton).getName());
do {
secondButton = r.nextInt(list.size());
}
while (secondButton == firstButton);
do {
thirdButton = r.nextInt(list.size());
} while (thirdButton == firstButton || thirdButton == secondButton);
do {
fourthButton = r.nextInt(list.size());
}
while (fourthButton == firstButton || fourthButton == secondButton || fourthButton == thirdButton);
b_answer2.setText(list.get(secondButton).getName());
b_answer3.setText(list.get(thirdButton).getName());
b_answer4.setText(list.get(fourthButton).getName());
break;
case 1:
b_answer2.setText(list.get(secondButton).getName());
do {
secondButton = r.nextInt(list.size());
}
while (secondButton == firstButton);
do {
thirdButton = r.nextInt(list.size());
} while (thirdButton == firstButton || thirdButton == secondButton);
do {
fourthButton = r.nextInt(list.size());
}
while (fourthButton == firstButton || fourthButton == secondButton || fourthButton == thirdButton);
b_answer1.setText(list.get(firstButton).getName());
b_answer3.setText(list.get(thirdButton).getName());
b_answer4.setText(list.get(fourthButton).getName());
break;
case 2:
b_answer3.setText(list.get(thirdButton).getName());
do {
secondButton = r.nextInt(list.size());
}
while (secondButton == firstButton);
do {
thirdButton = r.nextInt(list.size());
} while (thirdButton == firstButton || thirdButton == secondButton);
do {
fourthButton = r.nextInt(list.size());
}
while (fourthButton == firstButton || fourthButton == secondButton || fourthButton == thirdButton);
b_answer2.setText(list.get(secondButton).getName());
b_answer1.setText(list.get(firstButton).getName());
b_answer4.setText(list.get(fourthButton).getName());
break;
case 3:
b_answer4.setText(list.get(fourthButton).getName());
do {
secondButton = r.nextInt(list.size());
}
while (secondButton == firstButton);
do {
thirdButton = r.nextInt(list.size());
} while (thirdButton == firstButton || thirdButton == secondButton);
do {
fourthButton = r.nextInt(list.size());
}
while (fourthButton == firstButton || fourthButton == secondButton || fourthButton == thirdButton);
b_answer1.setText(list.get(firstButton).getName());
b_answer2.setText(list.get(secondButton).getName());
b_answer3.setText(list.get(thirdButton).getName());
break;
}
}
答案 0 :(得分:0)
你需要在代码中纠正一件事。当你调用`r.nextInt(3)时,你会得到一个从0到2的数字。 所以在你的交换机中,case:3将永远不会被执行。 你能指定用于的号码吗?你的逻辑如何运作? 你有一个答案列表并设置按钮以包含从列表中取出的一个随机答案然后在每个按钮被设置后删除它会不会更简单吗?
像(伪代码)
之类的东西Declare list of answer
String answer1 = list.get(r.nextInt(list.size() -1))
Button1.setText(answer1);
list.remove(answer1);
ecc...
答案 1 :(得分:0)
在每种情况下,你确定第二个,第三个和第四个数字,并用第一个数字做一些奇怪的事情。例如:
case 1:
b_answer2.setText(list.get(secondButton).getName());
do {
secondButton = r.nextInt(list.size());
} while (secondButton == firstButton);
do {
thirdButton = r.nextInt(list.size());
} while (thirdButton == firstButton || thirdButton == secondButton);
do {
fourthButton = r.nextInt(list.size());
} while (fourthButton == firstButton || fourthButton == secondButton || fourthButton == thirdButton);
b_answer1.setText(list.get(firstButton).getName());
b_answer3.setText(list.get(thirdButton).getName());
b_answer4.setText(list.get(fourthButton).getName());
break;
由于您secondButton
影响b_answer2
。您的第一个do-while循环应该确定firstButton
而不是secondButton
。
或者您不应该将secondButton
影响到b_answer2
,而应该影响firstButton
并且不要更改您的do-while循环。
由于你没有很好地解释你的逻辑,我不知道这两种可能性中哪一种是正确的。