如何从数组中实现Fisher-Yates随机播放

时间:2019-07-13 03:02:20

标签: android-studio

我正在尝试在我的应用程序中实现Fisher-Yates随机播放,所以我在数组中有问题,有多种选择,我想随机播放这些问题 这是我的第一次在这里 谢谢

这是我的代码

QuizActivity.java

public class QuizActivity extends AppCompatActivity {

    private QuestionBank mQuestionLibrary = new QuestionBank();
    private TextView mScoreView;
    private TextView mScoreView2;// view for current total score
    private TextView mQuestionView;  //current question to answer
    private Button mButtonChoice1; // multiple choice 1 for mQuestionView
    private Button mButtonChoice2; // multiple choice 2 for mQuestionView
    private Button mButtonChoice3; // multiple choice 3 for mQuestionView
    private String mAnswer;  // correct answer for question in mQuestionView
    private int mScore = 0;  // current total score
    private int mQuestionNumber = 0; // current question number

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        // setup screen for the first question with four alternative to answer
        mScoreView = (TextView)findViewById(R.id.score);
        mScoreView2 = (TextView)findViewById(R.id.score_text);
        mQuestionView = (TextView)findViewById(R.id.question);
        mButtonChoice1 = (Button)findViewById(R.id.choice1);
        mButtonChoice2 = (Button)findViewById(R.id.choice2);
        mButtonChoice3 = (Button)findViewById(R.id.choice3);
        Typeface custom_font = Typeface.createFromAsset(getAssets(),  "fonts/whale.ttf");
        mScoreView.setTypeface(custom_font);
        mScoreView2.setTypeface(custom_font);
        mQuestionView.setTypeface(custom_font);
        mButtonChoice1.setTypeface(custom_font);
        mButtonChoice2.setTypeface(custom_font);
        mButtonChoice3.setTypeface(custom_font);
        updateQuestion();
        // show current total score for the user
        updateScore(mScore);
    }


    private void updateQuestion(){

        // check if we are not outside array bounds for questions
        if(mQuestionNumber<mQuestionLibrary.getLength() ){
            // set the text for new question, and new 4 alternative to answer on four buttons
            mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
            mButtonChoice1.setText(mQuestionLibrary.getChoice(mQuestionNumber, 1));
            mButtonChoice2.setText(mQuestionLibrary.getChoice(mQuestionNumber, 2));
            mButtonChoice3.setText(mQuestionLibrary.getChoice(mQuestionNumber, 3));
            mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
            mQuestionNumber++;
        }
        else {
            Toast.makeText(QuizActivity.this, "It was the last question!", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(QuizActivity.this, HighestScoreActivity.class);
            intent.putExtra("score", mScore); // pass the current score to the second screen
            startActivity(intent);
        }
    }

    // show current total score for the user
    private void updateScore(int point) {
        mScoreView.setText("" + mScore+"/"+mQuestionLibrary.getLength());
    }

    public void onClick(View view) {
        //all logic for all answers buttons in one method
        Button answer = (Button) view;
        // if the answer is correct, increase the score
        if (answer.getText() == mAnswer){
            mScore = mScore + 1;
            Toast.makeText(QuizActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
        }else
            Toast.makeText(QuizActivity.this, "Wrong!", Toast.LENGTH_SHORT).show();
        // show current total score for the user
        updateScore(mScore);
        // once user answer the question, we move on to the next one, if any
        updateQuestion();
    }
}

QuestionBank.java

public class QuestionBank {

// array of questions
private String textQuestions [] = {
        "1. Apa bahasa inggris dari kucing ?",
        "2. Elephant adalah ?",
        "3. Apa bahasa inggris dari jerapah ?",

};

// array of multiple choices for each question
private String multipleChoice [][] = {
        {"Cat", "Dog", "Mouse"},
        {"Kucing", "Monyet", "Gajah"},
        {"Giraffe", "Monkey", "Lion"}
};

// array of correct answers - in the same order as array of questions
private String mCorrectAnswers[] = {"Cat", "Gajah", "Giraffe" };

// method returns number of questions
public int getLength(){
    return textQuestions.length;
}

// method returns question from array textQuestions[] based on array index
public String getQuestion(int a) {
    String question = textQuestions[a];
    return question;
}

// method return a single multiple choice item for question based on array index,
// based on number of multiple choice item in the list - 1, 2, 3 or 4 as an argument
public String getChoice(int index, int num) {
    String choice0 = multipleChoice[index][num-1];
    return choice0;
}

//  method returns correct answer for the question based on array index
public String getCorrectAnswer(int a) {
    String answer = mCorrectAnswers[a];
    return answer;
}
}

我正在尝试在我的应用程序中实现Fisher-Yates随机播放,所以我在数组中有问题,有多种选择,我想随机播放这些问题 这是我的第一篇文章

0 个答案:

没有答案