どのように私は、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]);

どのように私は、フラグ変数を定義し、また、ラジオボタンで回答を混在させることができます。

エリトリア:

まず作成Question単純化のskaeために、クラスを私は静的な内部クラスとしてそれを行います

次に、配列を作成(または質問のリストをオプション):

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

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=349957&siteId=1