2

I am a beginner in Android App development. The code below is a quiz app and I want it to loop random questions and don't repeat the question, I tried to use a flag2 to randomly generate questions but I was getting compile errors, can anyone help me out with this. I'm also a beginner in Java.

TextView tv;
Button btn1;
RadioButton rb1,rb2,rb3;
RadioGroup rg;

String Questions[]={"What is 1+1?","Capital of USA?","What is 2+2","Echo with Laughter","Warg"};
String opt[]={"2","3","4", "New York","Washington DC","Maryland", "5","4","6","Stairway to Heaven","Hotel California","Highway to hell","Jon","Bran","Dario" };
String ans[]={"2","Washington DC","4","Stairway to heaven","Bran"};



int flag=0;
public static int correct;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    tv=(TextView)findViewById(R.id.textView2);
    btn1=(Button)findViewById(R.id.button2);
    rg=(RadioGroup)findViewById(R.id.radioGroup);
    rb1=(RadioButton)findViewById(R.id.radioButton);
    rb2=(RadioButton)findViewById(R.id.radioButton2);
    rb3=(RadioButton)findViewById(R.id.radioButton3);

    tv.setText(Questions[flag]);
    rb1.setText(opt[0]);
    rb2.setText(opt[1]);
    rb3.setText(opt[2]);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
            String ansText = uans.getText().toString();

            if (ansText.equalsIgnoreCase(ans[flag])) {
                correct++;
            }
            else {
                Intent in = new Intent(getApplicationContext(),Token.class);
                startActivity(in);
            }
            flag++;
            if (flag < Questions.length) {
                tv.setText(Questions[flag]);
                rb1.setText(opt[flag * 3]);
                rb2.setText(opt[(flag * 3)+1]);
                rb3.setText(opt[(flag * 3)+2]);
            }
            else {

                Intent in = new Intent(getApplicationContext(),Token.class);
                startActivity(in);
            }

        }

4 Answers 4

1

use java.lang.Math.random(). it will return values from 0.0 to 0.1 convert those values in integer & get the question of the integer

Sign up to request clarification or add additional context in comments.

Comments

1

Generate random number within the range which here would be from 0 to Maximum number of question, you can do it like below:

int numberOfQuestion = 5; 
Random rn = new Random();
randomNum = rn.nextInt() % numberOfQuestion; // random number from 0 to MaxRange - 1

Instead of using three arrays you can create a class:

class Quiz{
    String question;
    String answer;
    String[] options;
    boolean asked;
}

And every time when the particular question is asked just make the flag asked to true, and before asking question just check if that question is asked or not, if not then only display the question.

Comments

1

Three string array actualy useless. You should use object ArrayList.

public class QuestionObject{
    int id;
    String question;
    String [] options;
    String answer;
    --you should implement getter and setter--
    public int getId(){
       return this.id;
    }
    public void setId(int id){
      this.id= id;
    }
    public String getQuestion(){
       return this.question;
    }
    public void setQuesion(String question){
      this.question = question;
    }
   public String[] getOptions() 
   { 
      return options; 
   } 

  public void setOptions(String[] options) 
 { 
   this.options= options; 
   } 

  public int getOptionsElement(int location) 
   { 
      return options[location]; 
  } 

  public void setOptionsElement(int value, int location) 
  { 
    options[location] = value; 
  }
    public String getAnswer(){
       return this.answer;
    }
    public void setAnswer(String answer){
      this.answer= answer;
    }
}

And you should use this like

ArrayList<QuestionObject> questionObject = new ArrayList<QuestionObject>();
ArrayList<QuestionObject> answeredQuestion = new ArrayList<QuestionObject>();

Don't forget fill the questionObject with your Question options and aswers.

After that your logic must be implement.you can take the id of question when question display and remove the list. May be you can push the removed question another Arraylist.

//take the diplaying question this id can be your random number
int id = questionObject.getId(); //or int id = yourRandomNumber;
//store the another arraylist this question
answeredQuestion.add(questionObject.get(id));
//remove it from list than never show again 
questionObject.remove(id);

I think this may help you.

Comments

0

EDIT:

This is due to while goes infinite, New declarations as Lists:

   Random random = new Random();
        String rightAnswer=null;
        List<String> questions=new ArrayList<String>(); // list of questions
        List<String> answers=new ArrayList<String>();  // list of answers

    String[] ques={"What is 1+1?","Capital of USA?","What is 2+2","Echo with Laughter","Warg"};
            questions.addAll(Arrays.asList(ques));

    String[] ans={"2","Washington DC","4","Stairway to heaven","Bran"};     
            answers.addAll(Arrays.asList(ans));

    ArrayList<String[]> options = new ArrayList<String[]>(); // list of arrays that holds options

    String[] opt1={"2","3","4"};
    String[] opt2={"New York","Washington DC","Maryland"};
    String[] opt3={"5","4","6"};
    String[] opt4={"Stairway to Heaven","Hotel California","Highway to hell"};
    String[] opt5={"Jon","Bran","Dario" };
    options.add(opt1);
    options.add(opt2);
    options.add(opt3);
    options.add(opt4);
    options.add(opt5);

And where you want to generate question you can write this code, I am assuming in onClick() of Button:

    int questionNumber;
    if(questions.size()>0) // only run if list contains something
    {

    questionNumber = random.nextInt(questions.size());  // generate random question from current list

    String[] currentOptions=options.get(questionNumber);

    tv.setText(questions[questionNumber]);
    rb1.setText(currentOptions[0]);
    rb2.setText(currentOptions[1]);
    rb3.setText(currentOptions[2]);


    rightAnswer=answers.get(questionNumber); // hold right answer in this variable

    questions.remove(questionNumber); // remove question which is asked
    answers.remove(questionNumber);   // remove answer which is asked
    options.remove(questionNumber);   // remove options that are showed   


    }
    else
    {
     tv.setText("No questions remaining");
    }

This is tested, and I was able to get these results: enter image description here

NOTE: Declaration part must be outside the function that generates random question, otherwise it will fail.

1 Comment

This worked for me, but I can't seem to go to the next question and it gives me the error "Too much output".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.