2016-12-30 4 views
-1

私は多肢選択問題を作成しようとしています。私は4つのランダムな回答を作成することができましたが、正しい答えをどのように入れるかはわかりません。それはオプション1,2,3、または4のいずれかでなければならず、ランダムでなければなりません。複数選択肢の回答の1つがアンドロイドで正しいことを保証するにはどうすればよいですか?

私の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にする方法はわかりません。 オプションとして正しい回答を設定したい(乱数)テキスト。

+0

正解の位置を取得する必要がありますか?なぜ、1-4の間の乱数を返さず、正解の位置としてその数字を使うのはなぜですか? –

答えて

0

私はあなたの質問の権利を持っていれば、いくつかのオプションで無作為に配置することができます。

同じことをする次の方法を使用できます。

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; 
    } 

このオプションの配列を使用してください。

+0

これは3つのランダムな答えと1つの正解の権利の配列を作成しますか?私は本当にintを理解することができませんでしたrandomPos =(新しいRandom()。nextInt())%4; part –

+0

randomPosは動作しません –

+0

私はそれを動作させましたが、重複を与えることがあり、プログラムを中断するように修正しようとすると –

0
 private int fetchCorrectAnswer() { 
      int correctAnswer = 4; 
      int option = new Random().nextInt(2); // outputs either 0 or 1 
      //if option is 1 which is randomly genereted, correctAnswer will be 4. 
      //if option is 0,then correctAnswer will be wither 1 or 2 or 3 which is also randomly generated 
      if(option == 0){ 
       //if 0, then it should be between 1/2/3 i.e min = 1, max = 3 
       correctAnswer = (new Random().nextInt(max - min + 1)) + 1; // +1 because 0-2 as 1-3 
      } 
      return correctAnswer; 
    } 
+0

これは私に1と4の間の乱数を与えるだけではありませんか? –

関連する問題