如何在Android中显示随机问题

时间:2020-03-22 21:20:47

标签: java android

我有一个关于在Android应用中显示随机问题的问题。所有问题均按顺序列出,并非随机列出。

这是我的代码段,如下所示。

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]);

如何定义标志变量以及如何在单选按钮中混合答案。

1 个答案:

答案 0 :(得分:1)

首先创建一个Question类,为简单起见,我将其作为静态内部类进行

然后创建一个数组(或可选的问题列表):

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));
    }
}
相关问题