private void getAnswers(int Type) {
/*
* Getting answers here
*/
//int randomValue = random(4);
try {
String answers_list[][] = {
{"Answer 1-1", "Answer 2-1", "Answer 3-1", "Answer 4-1"},
{"Answer 1-2", "Answer 2-2", "Answer 3-2", "Answer 4-2"},
{"Answer 1-3", "Answer 2-3", "Answer 3-3", "Answer 4-3"},
{"Answer 1-4", "Answer 2-4", "Answer 3-4", "Answer 4-4"},
{"Answer 1-5", "Answer 2-5", "Answer 3-5", "Answer 4-5"}} ;
//answer1.setText(answers_list[Type][randomValue+1 > 3 ? (randomValue+0)-4 : randomValue+0]);
//answer2.setText(answers_list[Type][randomValue+2 > 3 ? (randomValue+1)-3 : randomValue+1]);
//answer3.setText(answers_list[Type][randomValue+3 > 3 ? (randomValue+2)-2 : randomValue+2]);
//answer4.setText(answers_list[Type][randomValue+0 > 3 ? (randomValue+3)-4 : randomValue+3]);
Collections.shuffle(Arrays.asList(answers_list[Type]));
answer1.setText(answers_list[Type][0]);
answer2.setText(answers_list[Type][1]);
answer3.setText(answers_list[Type][2]);
answer4.setText(answers_list[Type][3]);
/*for (int rows = 0; rows < answer&*list.length; rows++){
for (int cols = 0; cols < answers_list[rows].length; cols++){
}
}*/
} catch(Exception ex){
answer1.setText("Error "+ex);
}
}
我想"Answer 1-1"
,"Answer 1-2"
,"Answer 1-3"
,"Answer 1-4"
,"Answer 1-5"
是正確的答案,我使用這個檢查答案的正確與否......
switch(view.getId()){
case R.id.button5:
clicks++;
if(clicks > 0)
beginn.setText("Next");
break;
case R.id.button1:
if(answer == 1)
correct++;
else
correct--;
break;
case R.id.button2:
if(answer == 2)
correct++;
else
correct--;
break;
case R.id.button3:
if(answer == 3)
correct++;
else
correct--;
break;
case R.id.button4:
if(answer == 4)
correct++;
else
correct--;
break;
基本上,我需要將我的answer
整數設置爲某個值,我該怎麼做?
編輯:我知道這是answer = number;
,但是什麼數字?在我的數組列表被混洗之後,我如何檢查它是否爲"Answer 1-1"
?如果沒有做這樣的事情:
if(answers_list[Type][0] == "Answer 1-1")
answer = 1;
else if(answers_list[Type][1] == "Answer 1-1")
answer = 2;
那我該如何隨機化呢?你能告訴我一個代碼示例嗎? – Alex
@John:我自己,我會創建一個ArrayList,將所有可能的答案字符串添加到數組列表中,但正確的答案和所有不正確的答案,然後只需調用java.util.Arrays.shuffle(...)我的ArrayList。簡單。 –