Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中显示随机问题_Java_Android - Fatal编程技术网

Java 如何在Android中显示随机问题

Java 如何在Android中显示随机问题,java,android,Java,Android,我在Android应用程序中显示随机问题时遇到问题。所有问题都按顺序列出,而不是随机列出 下面是我的代码片段,如下所示 String questions[] = { "Which method can be defined only once in a program?", "Which of these is not a bitwise operator?",

我在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]);
字符串问题[]={
“哪个方法在程序中只能定义一次?”,
“以下哪项不是位运算符?”,
“方法使用哪个关键字引用调用它的对象?”,
“以下哪个关键字用于定义Java中的接口?”,
“以下哪些访问说明符可用于接口?”,
“以下哪项是导入整个软件包‘pkg’的正确方法?”,
“构造函数的返回类型是什么?”,
“以下哪个包存储所有标准java类?”,
“以下哪个类字符串方法用于比较两个字符串对象的相等性?”,
涉及byte、int和literal数字的表达式升级为以下哪一个
};

String answers[]={“main method”,“首先创建一个
问题
类,为了简单起见,我将把它作为一个静态的内部类来做

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

导入java.util.array;
导入java.util.List;
导入java.util.Random;
公共类MyClass{
静态类问题{
字符串问题;
字符串回答;
列出选项;
公共问题(字符串问题、字符串答案、列表选项){
这个问题=问题;
这个答案=答案;
this.options=选项;
}
公共字符串getQuestion(){
返回问题;
}
公共问题(字符串问题){
这个问题=问题;
}
公共字符串getAnswer(){
返回答案;
}
公共无效设置应答(字符串应答){
这个答案=答案;
}
公共列表getOptions(){
返回选项;
}
公共无效设置选项(列表选项){
this.options=选项;
}        
}
公共静态void main(字符串[]args){
问题[]我的问题=
{新问题(“哪个方法在程序中只能定义一次?”,
“主要方法”,
asList(“finalize方法”、“main方法”、“static方法”、“private方法”),
新问题(“其中哪一个不是位运算符?”,

“您(至少)要处理两个问题:1)使用Java
Random()
r.nextInt(int n)
生成随机索引。2)您需要跟踪用户选择的索引,以确保(现在是随机的!)问答匹配。查找前者。对于后者,我建议将随机整数写入查找表,例如
int[]index=new int[questions.length];
或将问题[]、答案[]和索引[]组合在一起合并到一个新类中。我会使用二维数组来处理问题和选项,或者更好地使用“问题对象”,将问题、答案和选项作为字段。@FoggyDay我如何修复它?@Eritrian如果您不介意的话,是否可以编写有关它的代码片段。
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));
    }
}