2016-12-30 50 views
-1

我正試圖創建一個多選題數學問題。我設法創建了4個隨機答案,但我不確定如何輸入正確的答案。它必須是選項1,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的隨機數,但我不知道如何使用它來使correctanswer轉到選項1,2,3或4. 我想要將correctanswer設置爲選項(randomnumber)文本。

+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個隨機答案和一個正確答案的數組嗎?我無法真正理解int randomPos =(new Random()。nextInt())%4;部分 –

+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之間的隨機數嗎? –

相關問題