数组中的随机问题

时间:2017-07-15 16:17:56

标签: java android arrays shuffle

我在另一个java类中创建了三个Arrays:Question Array,Choices Array,Answers Array。现在我希望当按下选项(错误或错误)时,theese问题将随机随机播放我的代码首先是问题图书馆,接下来是主要活动。

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;

import java.util.ArrayList; import 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;
}

}

3 个答案:

答案 0 :(得分:1)

将您的问题和答案保存在不同的数组中并不是一个好主意,因为您无法重新连接它们,而是将数据重新排列在一起。 我建议像这样写一个#include <math.h> #include <stdio.h> #ifndef M_PI #define M_PI 3.14159265358979323846 #endif struct cylinder { double radius; double height; double weight; double volume; }; int cylinder_cmp(const void *p1, const void *p2) { /* sort cylinders by increasing volume */ const struct cylinder *c1 = p1; const struct cylinder *c2 = p2; return (c1->volume > c2->volume) - (c1->volume < c2->volume); } int main(void) { FILE *cFileIn, *cFileOut; double radius, height, weight, volume; struct cylinder my_cylinders[100]; cFileIn = fopen("cylinders.txt", "r"); if (cFileIn == NULL) { printf("Error opening file \n"); return 1; } cFileOut = fopen("sorted_cylinders.txt", "w"); if (cFileOut == NULL) { printf("Error opening file \n"); return 1; } int i, counter; for (counter = 0; counter < 100; counter++) { /* read value from the file, ignoring the volume */ if (fscanf(cFileIn, "%lf, %lf, %lf", &radius, &height, &weight) == 3) { my_cylinders[i].radius = radius; my_cylinders[i].height = height; my_cylinders[i].weight = weight; my_cylinders[i].volume = M_PI * radius * radius * height; } else { break; } } fclose(cFileIn); qsort(my_cylinders, counter, sizeof(*my_cylinders), cylinder_cmp); fprintf(cFileOut, "# Radius Height Volume Weight\n"); for (i = 0; i < counter; i++) { fprintf(cFileOut, "%-d\t %-12.6lf\t %-12.6lf\t %-12.6lf\t %-12.6lf\n", counter, my_cylinders[counter].radius, my_cylinders[counter].height, my_cylinders[counter].volume, my_cylinders[counter].weight); } fclose(cFileOut); printf("File sorted_cylinders.txt written\n"); return 0; } 类:

Question

然后使用List来管理问题:

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

}
编辑:当然,关于这个Questions类还有很多需要改进的地方 - 比如最好有一个 //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); 方法,而不必将一个字符串数组传递给构造函数。但那取决于你:)

答案 1 :(得分:0)

(真棒)方式:亲自实施 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));

答案 2 :(得分:0)

使用ArrayList代替并执行此操作:

Networks