我正在尝试使用multidimension字符串数组制作一个简单的MCQ应用程序。
这是我的MainActivity
代码:
我的问题是如何定义答案数组。我做了补充,但它没有工作。
public class MainActivity extends Activity {
RadioGroup radioGroup;
RadioButton rb1, rb2, rb3, rb4;
TextView tvquestion, tvtotallength_yy, tvpresentindex_xx;
Button previous, answer, next, result;
int q, a, wrong, count;
String[][] array = {{"Question 1", "Question 2", "Question 3", "Question 4", "Question 5", "Question 6", "Question 7", "Question 8", "Question 9", "Question 10"},
{
"1answerA", "1answerB", "1answerC", "1answerD",
"2answerA", "2answerB", "2answerC", "2answerD",
"3answerA", "3answerB", "3answerC", "3answerD",
"4answerA", "4answerB", "4answerC", "4answerD",
"5answerA", "5answerB", "5answerC", "5answerD",
"6answerA", "6answerB", "6answerC", "6answerD",
"7answerA", "7answerB", "7answerC", "7answerD",
"8answerA", "8answerB", "8answerC", "8answerD",
"9answerA", "9answerB", "9answerC", "9answerD",
"10answerA", "10answerB", "10answerC", "10answerD",
}
};
String[] ans = {"1answerA", "2answerD", "3answerC", "4answerA", "5answerB", "6answerB", "7answerA", "8answerC", "9answerD", "10answerD"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialing text view
tvquestion = (TextView) findViewById(R.id.tvquestion);
tvtotallength_yy = (TextView) findViewById(R.id.tvyy);
tvpresentindex_xx = (TextView) findViewById(R.id.tvxx);
//initializing button
previous = (Button) findViewById(R.id.previous);
answer = (Button) findViewById(R.id.answer);
next = (Button) findViewById(R.id.next);
result = (Button) findViewById(R.id.result);
//initializing radio button
radioGroup = (RadioGroup) findViewById(R.id.radiogrp);
rb1 = (RadioButton) findViewById(R.id.optionA);
rb2 = (RadioButton) findViewById(R.id.optionB);
rb3 = (RadioButton) findViewById(R.id.optionC);
rb4 = (RadioButton) findViewById(R.id.optionD);
count = 0;
wrong = 0;
q = 0;
a = 0;
tvquestion.setText(array[0][q]);
rb1.setText(array[1][q]);
rb2.setText(array[1][q+1]);
rb3.setText(array[1][q+2]);
rb4.setText(array[1][q+3]);
tvpresentindex_xx.setText(String.valueOf(q + 1));
//setting the button to perform specific action
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
q--;
if (q == -1) {
q = array.length - 1;
tvquestion.setText(array[0][q]);
rb1.setText(array[1][q]);
rb2.setText(array[1][q + 4]);
rb3.setText(array[1][q + 8]);
rb4.setText(array[1][q + 12]);
tvpresentindex_xx.setText(String.valueOf(q + 1));
} else {
tvquestion.setText(array[0][q]);
rb1.setText(array[1][a]);
rb2.setText(array[1][q + 4]);
rb3.setText(array[1][q + 8]);
rb4.setText(array[1][q + 12]);
tvpresentindex_xx.setText(String.valueOf(q + 1));
}
}
});
answer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.optionA:
if (ans[q] == array[1][q]) {
count++;
} else {
wrong++;
}
break;
case R.id.optionB:
if (ans[q] == array[1][q + 4]) {
} else {
wrong++;
}
break;
case R.id.optionC:
if (ans[q] == array[1][q + 8]) {
} else {
wrong++;
}
break;
case R.id.optionD:
if (ans[q] == array[1][q + 12]) {
} else {
wrong++;
}
break;
}
}
});
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
radioGroup.clearCheck();
q++;
if (q == array.length) {
q = 0;
tvquestion.setText(array[0][q]);
rb1.setText(array[0][q]);
rb2.setText(array[0][q + 4]);
rb3.setText(array[0][q + 8]);
rb4.setText(array[0][q + 12]);
tvpresentindex_xx.setText(String.valueOf(q + 1));
} else {
tvquestion.setText(array[0][q]);
rb1.setText(array[1][q]);
rb2.setText(array[1][q + 4]);
rb3.setText(array[1][q + 8]);
rb4.setText(array[1][q + 12]);
tvpresentindex_xx.setText(String.valueOf(q + 1));
}
}
});
result.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Correct=" + count + " Incorrect=" + wrong, Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:0)
而不是存储实际答案
String[] ans = {"1answerA", "2answerD", "3answerC", "4answerA", "5answerB", "6answerB", "7answerA", "8answerC", "9answerD", "10answerD"};
你可以像这样存储答案的索引。假设您有10个问题和4个选项存储在数组中。所以你可以像这样定义一个int数组的答案
int[] answers_index = new int[]{1,2,2,3,1,1,3,3,1,4,4};
答案 1 :(得分:0)
您可以尝试将答案的索引存储为整数数组。例如,如果您的答案分别是4个问题的a,d,c和b,则答案数组可以是 [0,3,2,1]。然后对单选按钮进行编号,使按钮1具有0表示,依此类推。最后,当用户输入答案时,您可以将按钮的数字表示与答案数组中的整数值进行比较,以检查是否正确。
希望这有帮助!