Я пытаюсь создать математический вопрос с множественным выбором. Мне удалось создать 4 случайных ответа, но я не уверен, как правильно ответить. Он должен быть либо option1,2,3, либо 4, и он должен быть случайным.Как гарантировать, что один из ответов на множественный выбор правильный в Android?
Мой код Java:
public class MainActivity extends AppCompatActivity{
private boolean correct;
private String questionTxt, option1String, option2String, option3String, option4String;
private int answer;
TextView option1Text, option2Text, option3Text, option4Text;
int r = (int) (Math.random() * (4 - 1)) + 1;
public void correctAnswer()
{
int first = (int)(Math.random() * 10);
int second = (int)(Math.random() * 10);
answer = first + second;
questionTxt = first + " + " + second + " = ?";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
/*ImageView rocketImage = (ImageView) findViewById(R.id.img_animation);
rocketImage.setBackgroundResource(R.drawable.greencartooncar);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();*/
TextView questionText = (TextView)findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(wrongAnswer() + "");
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(wrongAnswer() + "");
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(wrongAnswer() + "");
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(wrongAnswer() + "");
setString();
if (option1String.equals(option2String)||option1String.equals(option3String)||option1String.equals(option4String)
||option2String.equals(option3String)||option2String.equals(option4String)||option3String.equals(option4String))
{
noSameAnswer();
}
}
public int wrongAnswer()
{
return (int)(Math.random() * 10 + Math.random() * 10);
}
public void onClick3(View v) {
if (option3String.equals(Integer.toString(answer))) {
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(wrongAnswer() + "");
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(wrongAnswer() + "");
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(wrongAnswer() + "");
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(wrongAnswer() + "");
setString();
noSameAnswer();
}
}
public void onClick1(View v) {
if (option1String.equals(Integer.toString(answer))) {
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(wrongAnswer() + "");
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(wrongAnswer() + "");
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(wrongAnswer() + "");
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(wrongAnswer() + "");
setString();
noSameAnswer();
}
}
public void onClick2(View v) {
if (option2String.equals(Integer.toString(answer))) {
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(wrongAnswer() + "");
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(wrongAnswer() + "");
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(wrongAnswer() + "");
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(wrongAnswer() + "");
setString();
noSameAnswer();
}
}
public void onClick4(View v) {
if (option4String.equals(Integer.toString(answer))) {
TextView questionText = (TextView) findViewById(R.id.question);
correctAnswer();
questionText.setText(questionTxt + "");
option1Text = (TextView)findViewById(R.id.option1);
option1Text.setText(wrongAnswer() + "");
option2Text = (TextView)findViewById(R.id.option2);
option2Text.setText(wrongAnswer() + "");
option3Text = (TextView)findViewById(R.id.option3);
option3Text.setText(wrongAnswer() + "");
option4Text = (TextView)findViewById(R.id.option4);
option4Text.setText(wrongAnswer() + "");
setString();
noSameAnswer();
}
}
}
Некоторые из ненужных методов вырезают. Поле int r - это случайное число от 1 до 4, но я не знаю, как использовать это, чтобы сделать правильный ответ на вариант 1,2,3 или 4. Я хочу, чтобы correctanswer был установлен как опция (randomnumber) Текст.
Вам нужно получить правильную позицию? Почему бы просто не вернуть случайное число между 1-4 и использовать этот номер в качестве правильного ответа? –