-4

I have a problem about showing random question in my Android app. All questions are listed orderly not randomly.

Here is my code snippet shown below.

String questions[] = {
                            "Which method can be defined only once in a program?",
                            "Which of these is not a bitwise operator?",
                            "Which keyword is used by method to refer to the object that invoked it?",
                            "Which of these keywords is used to define interfaces in Java?",
                            "Which of these access specifiers can be used for an interface?",
                            "Which of the following is correct way of importing an entire package ‘pkg’?",
                            "What is the return type of Constructors?",
                            "Which of the following package stores all the standard java classes?",
                            "Which of these method of class String is used to compare two String objects for their equality?",
                            "An expression involving byte, int, & literal numbers is promoted to which of these?"
                         };
    String answers[] = {"main method","<=","this","interface","public","import pkg.*","None of the mentioned","java","equals()","int"};
    String opt[] = {
                    "finalize method","main method","static method","private method",
                    "&","&=","|=","<=",
                    "import","this","catch","abstract",
                    "Interface","interface","intf","Intf",
                    "public","protected","private","All of the mentioned",
                    "Import pkg.","import pkg.*","Import pkg.*","import pkg.",
                    "int","float","void","None of the mentioned",
                    "lang","java","util","java.packages",
                    "equals()","Equals()","isequal()","Isequal()",
                     "int","long","byte","float"
                   };
    int flag=0;

tv=(TextView) findViewById(R.id.tvque);
 rb1=(RadioButton)findViewById(R.id.radioButton);
        rb2=(RadioButton)findViewById(R.id.radioButton2);
        rb3=(RadioButton)findViewById(R.id.radioButton3);
        rb4=(RadioButton)findViewById(R.id.radioButton4);
        tv.setText(questions[flag]);
        rb1.setText(opt[0]);
        rb2.setText(opt[1]);
        rb3.setText(opt[2]);
        rb4.setText(opt[3]);

How can I define flag variable and also mix answers in Radio Buttons.

S.N
  • 2,157
  • 3
  • 29
  • 78
  • 1
    You have (at least) two issues to deal with: 1) use Java `Random()` and `r.nextInt(int n)` to generate random indexes. 2) You need to keep track of *which* index the user chooses, in order to ensure the (now random!) questions and answers match. Look [here](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) for the former. For the latter, I'd suggest writing your random ints into a lookup table, for example `int[] indexes = new int[questions.length];` Or combine questions[], answers[] and indexes[] together into a new class. – FoggyDay Mar 22 '20 at 21:25
  • 1
    I would use two demensional arrays for questions and options, or even better a 'question object' with question, answer and options as fields. – Eritrean Mar 22 '20 at 21:29
  • @FoggyDay How can I fix it? – S.N Mar 22 '20 at 21:31
  • @Eritrean Is it possible to write code snippet about it if you don't mind. – S.N Mar 22 '20 at 21:32

1 Answers1

1

First create a Question class, for the skae of simplicity I will do it as a static inner class

Then create an array (or optional a list of questions):

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class MyClass {

    static class Question{
        String question;
        String answer;
        List<String> options;

        public Question(String question, String answer, List<String> options) {
            this.question = question;
            this.answer = answer;
            this.options = options;
        }

        public String getQuestion() {
            return question;
        }

        public void setQuestion(String question) {
            this.question = question;
        }

        public String getAnswer() {
            return answer;
        }

        public void setAnswer(String answer) {
            this.answer = answer;
        }

        public List<String> getOptions() {
            return options;
        }

        public void setOptions(List<String> options) {
            this.options = options;
        }        
    }

    public static void main(String[] args) {        
        Question[] myQuestions = 
        {new Question("Which method can be defined only once in a program?",
                      "main method",
                      Arrays.asList("finalize method","main method","static method","private method")),
            new Question( "Which of these is not a bitwise operator?",
                      "<=",
                      Arrays.asList("&","&=","|=","<=")),
            new Question("Which keyword is used by method to refer to the object that invoked it?",
                      "this",
                      Arrays.asList("import","this","catch","abstract")),
            new Question("Which of these keywords is used to define interfaces in Java?",
                      "interface",
                      Arrays.asList("Interface","interface","intf","Intf")),
        };

        Random rand = new Random();
        int randomIndex = rand.nextInt(myQuestions.length);

        //hier goes your code for the text view and radio buttons   
        Question q = myQuestions[randomIndex];
        tv.setText(q.getQuestion());
        rb1.setText(q.getOptions().get(0));
        rb2.setText(q.getOptions().get(1));
        rb3.setText(q.getOptions().get(2));
        rb4.setText(q.getOptions().get(3));
    }
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28