Java 如何保证android中的多项选择答案之一是正确的?

Java 如何保证android中的多项选择答案之一是正确的?,java,android,multiple-choice,Java,Android,Multiple Choice,我在试着做一道数学选择题。我已经设法创建了4个随机答案,但我不知道如何输入正确的答案。它必须是选项1、2、3或4,并且应该是随机的 我的java代码: public class MainActivity extends AppCompatActivity{ private boolean correct; private String questionTxt, option1String, option2String, option3String, option4String; private

我在试着做一道数学选择题。我已经设法创建了4个随机答案,但我不知道如何输入正确的答案。它必须是选项1、2、3或4,并且应该是随机的

我的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。
我想将正确答案设置为选项(随机数)文本。

如果我答对了你的问题,你想将正确答案随机放置在某个选项上

您可以使用以下方法,该方法也可以这样做

private String[] makeOptions(int correctAns) {

        String [] ansStrins = new String[4];
//        for (int i=0;i<ansStrins.length;i++){
//            System.out.println("Option "+i+ ": "+ansStrins[i]);
//
//        }
        int randomPos = (new Random().nextInt())%4;
        ansStrins[randomPos]=correctAns+"";
        for (int i=0;i<4;i++){
            int randomAns = (int)(Math.random() * 10 + Math.random() * 10);
           if(ansStrins[i]==null){
               ansStrins[i]=randomAns+"";
           }
        }
        return ansStrins;
    }
private String[]makeOptions(int-correctAns){
字符串[]ansStrins=新字符串[4];

//为了(int i=0;i您需要获得正确答案的位置?为什么不返回一个介于1-4之间的随机数,并使用该数字作为正确答案的位置?这不就是给我一个介于1和4之间的随机数吗?因此这会创建一个包含3个随机答案和一个正确答案的数组?我无法真正理解int randomPos=(new Random().nextInt())%4;partI使它工作,但它有时会提供重复项,如果我试图修复,它会破坏程序
private String[] makeOptions(int correctAns) {

        String [] ansStrins = new String[4];
//        for (int i=0;i<ansStrins.length;i++){
//            System.out.println("Option "+i+ ": "+ansStrins[i]);
//
//        }
        int randomPos = (new Random().nextInt())%4;
        ansStrins[randomPos]=correctAns+"";
        for (int i=0;i<4;i++){
            int randomAns = (int)(Math.random() * 10 + Math.random() * 10);
           if(ansStrins[i]==null){
               ansStrins[i]=randomAns+"";
           }
        }
        return ansStrins;
    }