【问题标题】:Shuffle questions which are in an Array随机播放数组中的问题
【发布时间】:2017-07-15 16:17:55
【问题描述】:

我在另一个 java 类中创建了三个数组:问题数组、选择数组、答案数组。现在我希望这些问题在按下选择(错误或错误)时随机随机播放 我的代码首先是 QuestionLibrary,然后是主要活动。

package amapps.impossiblequiz;

public class QuestionLibrary {





    private String mQuestions[] = {
            "When was the European Union founded?",
            "How many Grad Celsius is one Kelvin?",
            "What is THC?",
            "How many legs has a spider?",
            "How many stars has the European flag?",
            "Which is the seventh planet from the sun?",
            "What is the chemical formula of salt?",
            "Who said: Ich bin ein berliner?",
            "To which country belongs Greenland?",
            "What is the result of: 2 + 2 *5?",
            "How many mountains are higher than 8000 meter/26.246 ft?",
            "A famous quote is: to be, or____ to be!",
            "What is the name of Stalingrad nowadays?"

    };

    private String mChoices[][] = {
            {"1993", "1986", "1967"},
            {"-260", "-272,15", "279,15"},
            {"a plant","The active substance of marijuana" , "a spider"},
            {"6", "10","8"},
            {"12","15","10"},
            {"Uranus","Neptune","Saturn"},
            {"HCl","NaCl","CO"},
            {"John F. Kennedy", "Richard Nixon","James A. Garfield"},
            {"Canada","Denmark", "Greenland is an own state?"},
            {"12","20","14"},
            {"10","12","14"},
            {"not","never","now"},
            {"Leningrad","Wolgograd","Dimitrijgrad"}
    };


    private String mCorrectAnswers[] = {"1993", "-272,15", "The active substance of marijuana", "8", "12","Uranus","NaCl","John F. Kennedy","Denmark","12","14","not","Wolgograd"};



    public String getQuestion (int a){
        String question = mQuestions[a];
        return question;
    }

    public String getChoice1 (int a){
        String choice0 = mChoices[a][0];
        return choice0;
}

    public String getChoice2 (int a) {
        String choice1 = mChoices[a][1];
        return choice1;
    }

    public String getChoice3 (int a) {
        String choice2 = mChoices [a] [2];
        return choice2;
    }

    public String getCorrectAnswer (int a){
        String answer = mCorrectAnswers [a];
        return answer;


    }



}

如果正确= 来自数组的随机问题/如果也是错误的

        mButtonChoice1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //My logic for Button goes in here

                if (mButtonChoice1.getText() == mAnswer) {
                    mScore = mScore + 1;
                    updateScore(mScore);
                    updateQuestion();

                    //This line of code is optional...
                    Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show();
                    mScore = 0;
                    updateScore(mScore);
                    updateQuestion();

                }
            }


        });

现在我尝试了一个新的代码来打乱我的数组,但是所有的都充满了错误......

包 amapps.impossiblequiz;

导入 java.util.ArrayList; 导入 java.util.List;

公开课问题{

    private String question;
    private String[] choices;
    private String answer;

    public Question(String question, String[] choices, String answer) {
        super();
        this.question = question;
        this.choices = choices;
        this.answer = answer;
    }

    public String getQuestion() {
        return question;
    }

    public String[] getChoices() {
        return choices;
    }

    public String getAnswer() {
        return answer;
    }

}

//create list
List<Question> questions = new ArrayList<Question>();

//add one question
questions.add(
        new Question(
                "What's you name?",
                        new String[]{"Foo","Bar","John","Doe"},
        "Bar"
        )
        );

//add another question
questions.add(
        new Question(
                "What's you name?",
                        new String[]{"Foo","Bar","John","Doe"},
        "Bar"
        )
        );

//shuffle questions
Collections.shuffle(questions);





public int getlength() {


    int length = 13;
    return length;
}

}

【问题讨论】:

标签: java android arrays shuffle


【解决方案1】:

将您的问题和答案保存在单独的数组中并不是一个好主意,因为您将无法将它们重新连接到您重新排列的数组中。 我建议像这样写一个Question 类:

public class Question {

    private String question;
    private String[] choices;
    private String answer;

    public Question(String question, String[] choices, String answer) {
        super();
        this.question = question;
        this.choices = choices;
        this.answer = answer;
    }

    public String getQuestion() {
        return question;
    }

    public String[] getChoices() {
        return choices;
    }

    public String getAnswer() {
        return answer;
    }

}

然后使用列表来管理问题:

    //create list
    List<Question> questions = new ArrayList<Question>();

    //add one question
    questions.add(
            new Question(
                    "What's you name?",
                    new String[]{"Foo","Bar","John","Doe"},
                    "Bar"
            )
    );

    //add another question
    questions.add(
            new Question(
                    "What's you name?",
                    new String[]{"Foo","Bar","John","Doe"},
                    "Bar"
            )
    );

    //shuffle questions
    Collections.shuffle(questions);

编辑:当然,这个 Questions 类还有很多需要改进的地方 - 例如,最好有一个 .addChoice(String choice) 方法,而不是必须将字符串数组传递给构造函数。但这取决于你:)

【讨论】:

  • 一些提示 - 您可以在 Question 构造函数中省略 super(),并使用菱形运算符 List&lt;Question&gt; questions = new ArrayList&lt;&gt;();
  • 我一定会测试这个,我需要在我的主要活动中改变一些东西吗?
  • 无法解析添加,无法解析simbol shuffle?我该怎么办?
  • @ProjectX 第二个代码块应该在方法内(主方法或其他方法,而不是直接在类体内)。 add() 是 ArrayList 的一个方法,它应该可以工作。
【解决方案2】:

改为使用 ArrayList 并执行以下操作:

Collections.shuffle(yourArrayListName);

【讨论】:

    【解决方案3】:

    (很棒的)方式:亲自实施 Fisher-Yates 洗牌。真的只有几行代码:

    // mutates original array
      private static <T> void shuffle(T[] arr){
        Random r = new Random();
        // iterates back to front
        for(int i=arr.length-1; i>0; i--){
          // swap
          int k = r.nextInt(i);
          T tmp = arr[k];
          arr[k] = arr[i];
          arr[i] = tmp;
        }
      }
    

    简单的方法: Collections.shuffle(Arrays.asList(mQuestions));

    【讨论】:

    • 你好冻结了,我应该把这个插入我的问题库吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多