2016-01-20 81 views
-1

我想使隨機測驗應用程序使用數組,所以我想保持重複。你能提出一些我可以實現的方法嗎?我已經添加了下面的代碼來獲得我所擁有的代碼。我不知道我需要做什麼。使用陣列隨機測驗應用程序使用數組

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.quiz); 
    QuestionMaker(); 
} 

此功能將使一大堆的問題:

//funtion makes the load of the question 

public void QuestionMaker(){ 
    QText=(TextView) findViewById(R.id.textView_questions); 
    Qgroup=(RadioGroup)findViewById(R.id.Group_radio); 
    btnNext=(Button)findViewById(R.id.button_next); 

    final ArrayList<String> distractorList = new ArrayList<String>(); 
    Collections.addAll(distractorList, Options); 


//first time of filling the form 

    int length = question.length; 
    for (int i = 0; i < length; i++) 
     quiz.add(new QuestionAndAnswer(question[i], answer[i], distractorList.subList(i * 3, (i + 1) * 3))); 
    Collections.shuffle(quiz); 
    fillInQuestion(); 


//clicking the radio button this happen 


    Qgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      makefinish++; 
      QuestionAndAnswer qna = quiz.get(currentQuestion); 
      qna.selectedAnswer = ((RadioButton) group.findViewById(checkedId)).getText().toString(); 

      if (qna.selectedAnswer == qna.answer) { 
       correctcounter++; 
       //correct message 

      } 
      if (!(qna.selectedAnswer == qna.answer)) { 
//wrong answer message 
      } 

} 

//function that makes the filling 

public void fillInQuestion() { 
    QuestionAndAnswer qna = quiz.get(currentQuestion);`enter code here` 
    QText.setText(qna.question); 

    int count = Qgroup.getChildCount(); 
    for (int i = 0; i < count; i++) 
     ((RadioButton) Qgroup.getChildAt(i)).setText(qna.allAnswers.get(i)); 


} 

回答

0

有幾個方法可以做到這一點。這裏是一個:

您可以隨機選擇一個問題,方法是隨機生成一個介於0和數組中元素個數之間的數字。之後,您可以在屏幕上顯示該問題。用戶回答問題後,要刷新屏幕並加載其他問題。你也應該創建另一個數組來存放已經生成的數字列表,並檢查問題編號以確保你之前沒有提出過這個問題。

要回答您關於重複的問題:在用戶輸入第一個問題的答案後,更改文本並回答選擇。這個邏輯也可以是循環或某種形式。