java - 为什么此方法每次都将第一个答案作为正确答案返回?
问题描述
我正在做一个欧洲国旗测验,newQuestion 方法旨在将所有 4 个按钮的答案选择随机排列,但第一个答案选择总是答案?
问题是案例陈述中的参数吗?是我在开始时定义每个按钮的时候吗?我该如何解决这个问题,以便答案选择在所有四个按钮之间有所不同,而不仅仅是第一个?
任何帮助,将不胜感激。
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;
}
}
解决方案
您必须在代码中更正一件事。当你调用 `r.nextInt(3); 你会得到一个从 0 到 2 的数字。所以在你的 switch 中, case: 3 永远不会被执行。你能指定使用什么号码吗?还有你的逻辑是如何工作的?如果您有一个答案列表并将按钮设置为包含从列表中获取的一个随机答案,然后在设置每个按钮后将其删除,这不是更简单吗?
类似的东西(伪代码)
Declare list of answer
String answer1 = list.get(r.nextInt(list.size() -1))
Button1.setText(answer1);
list.remove(answer1);
ecc...
推荐阅读
- activiti - 如何在不取消用户任务的情况下通过信号边界事件启动子进程
- python - Python:带有自定义标头的请求失败
- java - 扩展类的构造函数被调用两次
- javascript - 角度路由不显示模板
- dictionary - Thrift IDL 语法错误 - 接受并返回映射
- c# - 如何配置 swashbuckle 以显示 api 版本而不是 v{version} 变量?
- java - Vaadin 10 maven pom文件
- django - 如何在 Django Reversions 中有效地检索给定时间的属性值?
- c# - c# 通过与其他列表比较来删除列表中的元素
- javascript - `this.files = []` 在本地引发类型错误,但使用在线演示示例可以正常执行