Java – Displays a message when the answer is correct or incorrect

Displays a message when the answer is correct or incorrect… here is a solution to the problem.

Displays a message when the answer is correct or incorrect

I’m working on a quiz-based app. The quiz contains 1 question and 4 multiple-choice answers.

When the user selects any of the 4 options, something like “Your answer is correct” should be displayed if it is the correct answer, otherwise it should display “Your answer is wrong” along with the correct answer.

There are also 2 buttons (Next and Back), one for the next question and one for the back.

Can anyone tell me how to write code that returns the previous question?

I’ve done something like this:

 private void getShuffledArray() 
    {
         TODO Auto-generated method stub
        for (int i = 1; i <= SIZE; i++) 
        {
            quizIndexList.add(i);

}
        Collections.shuffle(quizIndexList);
        Log.d("ERR", "List A shuffling" + quizIndexList);

}

public void onClick(View v) {
         TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button1:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) {
                displayAnswer();

}

break;
        case R.id.button2:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) {
                displayAnswer();
            }
            break;

case R.id.button3:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) {
                displayAnswer();
            }
            break;

case R.id.button4:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) {
                displayAnswer();
            }
            break;

case R.id.btn_next:
          lyt_ans.setVisibility(View.GONE);
          lyt_quest.setVisibility(View.VISIBLE);
            counter += 1;
            if (counter >= SIZE) {
                Collections.shuffle(quizIndexList);
                counter = 0;
            }
            getInfoFromDB(quizIndexList.get(counter));
            reLoad();
            break;

case R.id.btn_bck:
             btn_next.setOnClickListener(new OnClickListener() 
               {

public void onClick(View v) 
                {
                     TODO Auto-generated method stub

finish();
                }
               });

}

Thank you very much for your help, thank you in advance.

Solution

@Rithesh – didn’t get your code, but had simple logic like this
What do you need
1) Array of questions
2) Answer Arraylist (or two-dimensional array) something like ArrayList<ArrayList<String>>
3) Index of answers to each question (It should match the question index.)
4) A View with a TextView (display problem), RadioGroup contains 4 radio buttons so that it can only select one at a time
5) Two buttons for the next and previous ones
6) Mainly add setOnCheckedChangeListener for your radio group, which will only trigger when the user selects any answer and then checks which radio button is selected (for this radio button this setting label to its position), so assuming the question number is 2 & radio The selected position is 3 then check your answer array for answer No & if it matches, you can display the toast & if it doesn’t match, then find the written answer from the Answers arraylist & display it in the toast or through an alert.
7) Now when you click the next button, change the text of the TextView to the next question, and change the text of the radio button to the next set of answers or vice versa so that the previous button is clicked

This is logical and cannot provide you with the code

Code to set the option

RadioButton option_rdb1 = (RadioButton) findViewById(R.id.option_rdb1);
setOptions(0);

 if you used ArrayList<ArrayList<String>> optionslist
public void setOptions(int index){
if((index-1) <= optionsList.size()){
    ArrayList<String> temp = optionslist.get(index);
    options_rdb1.setText(temp.get(0).toString();
    options_rdb1.setText(temp.get(0).toString();
    options_rdb1.setText(temp.get(0).toString();
    options_rdb1.setText(temp.get(0).toString();
  }
}

Simply call setOptions in next previous with the question index.
I used index-1 because arraylist starts with 0, so it depends on how you use the index.
In this way, you can set the first option similarly in the function that you will invoke on the next button, click Set next with the line of code above

Related Problems and Solutions