Im developing a quiz module using 1 String array for Questions , 1 two dim array for choices and 1 array for correct answers , here is my code
public void updateQuestion(){
int index=randomNumber.setRandomNo();
questionS.setText(cquestion.getQuestion(index));
ans1.setText(cquestion.getChoice1(index));
ans2.setText(cquestion.getChoice2(index));
ans3.setText(cquestion.getChoice3(index));
ans4.setText(cquestion.getChoice4(index));
correctAns=cquestion.getAnswer(index);
qNo++;
String q=String.valueOf(qNo);
questionNo.setText("C Language Test - Q"+q+"/Q3");//q=1
}
//Checking choice 1
public void choice1(View view){
if(ans1.getText().toString().equals(correctAns)){
score=score+1;
correctsound.start();
c1.setCardBackgroundColor(Color.parseColor("#27AE60"));//green
Toast.makeText(Hcl_Technical_CPapers.this,"Correct",Toast.LENGTH_SHORT).show();
delayQuestionupdate();
}
else{
wrongsound.start();
c1.setCardBackgroundColor(Color.parseColor("#E74C3C")); //red
Toast.makeText(Hcl_Technical_CPapers.this,"Not Correct",Toast.LENGTH_SHORT).show();
delayQuestionupdate();
}
}
//checking choice 2
public void choice2(View view){
if(ans2.getText().toString().equals(correctAns)){
score=score+1;
correctsound.start();
c2.setCardBackgroundColor(Color.parseColor("#27AE60"));
Toast.makeText(Hcl_Technical_CPapers.this,"Correct",Toast.LENGTH_SHORT).show();
delayQuestionupdate();
}
else{
wrongsound.start();
c2.setCardBackgroundColor(Color.parseColor("#E74C3C"));
Toast.makeText(Hcl_Technical_CPapers.this,"Not Correct",Toast.LENGTH_SHORT).show();
delayQuestionupdate();
}
}
public void choice3(View view){
if(ans3.getText().toString().equals(correctAns)){
score=score+1;
correctsound.start();
c3.setCardBackgroundColor(Color.parseColor("#27AE60"));
Toast.makeText(Hcl_Technical_CPapers.this,"Correct",Toast.LENGTH_SHORT).show();
delayQuestionupdate();
}
else{
c3.setCardBackgroundColor(Color.parseColor("#E74C3C"));
wrongsound.start();
Toast.makeText(Hcl_Technical_CPapers.this,"Not Correct",Toast.LENGTH_SHORT).show();
delayQuestionupdate();
}
}
//checking choice 4
public void choice4(View view){
if(ans4.getText().toString().equals(correctAns)){
score=score+1;
correctsound.start();
c4.setCardBackgroundColor(Color.parseColor("#27AE60"));
Toast.makeText(Hcl_Technical_CPapers.this,"Correct",Toast.LENGTH_SHORT).show();
delayQuestionupdate();
}
else{
c4.setCardBackgroundColor(Color.parseColor("#E74C3C"));
wrongsound.start();
Toast.makeText(Hcl_Technical_CPapers.this,"Not Correct",Toast.LENGTH_SHORT).show();
delayQuestionupdate();
}
}
public void checkQuestionLength(){
int l=cquestion.QArrayLength();
if(qNo>=l-1){
Intent intent=new Intent(Hcl_Technical_CPapers.this,TrackTest.class);
String s=String.valueOf(score);
intent.putExtra("score", s);
startActivity(intent);
Toast.makeText(Hcl_Technical_CPapers.this,"Score is "+s,Toast.LENGTH_SHORT).show();
}
else {
updateQuestion();
resetCardCColor();
}
}
public void delayQuestionupdate(){
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//2 sec gap
checkQuestionLength();
}
}, 2000);
}
}
here im passing the index variable(random each time) to the functions that are setting up the questions and choices in the views,everything is working fine but im getting repeated random questions , how can i get the index variable random and non repeatable , please help me out as im stucked in this for almost 2 days ,
Aucun commentaire:
Enregistrer un commentaire