简体   繁体   English

我如何从数组列表中获取正确的答案以显示在正确的按钮中

[英]How do i get the correct answer from an arraylist to show in the correct button

I am creating a quiz game which is working fine,but the problem am encountering is that whenever the user selects the correct answer, its shows it as wrong answer,meanwhile it's the correct answer, it's turns out the correct answer is not at the correct location. 我正在创建一个运行正常的测验游戏,但是遇到的问题是,每当用户选择正确的答案时,它就会显示为错误的答案,同时它是正确的答案,结果却是正确的答案并不正确。位置。

Here is my code so far, any ideas how i can fix this. 到目前为止,这是我的代码,我如何解决此问题的任何想法。

public class RiddleGuessActivity extends AppCompatActivity {
        TextView riddleQuestionTextView;
        Button btn1, btn2, btn3, btn4, btnContinue, btnOk;

        ArrayList<String> riddles = new ArrayList<>();
        ArrayList<String> answers = new ArrayList<>();
        int locationOfCorrectAnswers = 0,incorrectAnswers = 0;
        ArrayList<String> options = new ArrayList<>();
        LinearLayout gameOverLayout;
        LinearLayout buttonLayout;
        RelativeLayout resultLayout;
        TextView timerTextView;
        TextView answerTextView;
        TextView levelTextView, scoreTextView;
        TextView gameOverTextView;
        int levelCounter = 1, scoreCounter, riddleCounter = 0;

        public void chosenRiddle (View view) {

            if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswers))) {

                //Toast.makeText(RiddleGuessActivity.this, "Correct", Toast.LENGTH_SHORT).show();
                levelCounter++;
                scoreCounter+=60;
                riddleCounter++;
                levelTextView.setText(String.valueOf(levelCounter));
                scoreTextView.setText(String.valueOf(scoreCounter));

                result();

            }else {

                Toast.makeText(RiddleGuessActivity.this, "Wrong, It was " +
                        answers.get(riddleCounter), Toast.LENGTH_SHORT).show();
                gameOver();
            }
        }

        public void generateRiddles () {

            riddles.add("I kiss my mother before i die. What am i ?");
            riddles.add("It is greater than God and more evil than the devil. The poor have it, the rich need it and if you eat it you'll die. What am i ?");
            riddles.add("It walks on four legs in the morning, two legs at noon and three legs in the evening. What am i ?");
            riddles.add("I am the beginning of the end, and the end of time and space. I am essential to creation and i surround everyplace. What am i ?");
            riddles.add("What always runs but never walks, often murmurs, never talks, has a bed but never sleeps, has a mouth but never eats. What am i ?");
            riddles.add("At night they come without being fetched, By day they are lost without being stolen. What am i ?");
            riddles.add("The one who makes it, sells it. The one who buys it, never uses it. The one that uses it never knows that he's using it. What am i ?");
            riddles.add("The more you have of it, the less you see. What am i ?");
            riddles.add("I am always hungry, i must be fed, the finger i touch will soon turn red. What am i ?");
            riddles.add("If you break me, i do not stop working, if you touch me, i may snared, if you lose me, nothing will matter. What am i ?");

            answers.add("Matches");
            answers.add("Nothing");
            answers.add("Man");
            answers.add("Letter E");
            answers.add("River");
            answers.add("The Stars");
            answers.add("A Coffin");
            answers.add("Darkness");
            answers.add("Fire");
            answers.add("Heart");
            answers.add("The Moon");
            answers.add("Food");
            answers.add("Love");
            answers.add("Mouth");
            answers.add("Money");

            Random random = new Random();

            riddleQuestionTextView.setText(riddles.get(riddleCounter));

            locationOfCorrectAnswers = random.nextInt(4);

            options.clear();

            for (int i = 0; i < 4; i++) {

                if (i == locationOfCorrectAnswers &&
                        !options.contains(answers.get(riddleCounter))) {

                    options.add(answers.get(riddleCounter));

                }else {

                    incorrectAnswers = random.nextInt(answers.size());

                    while (incorrectAnswers == riddleCounter) {

                        incorrectAnswers = random.nextInt(answers.size());
                    }

                    if(!options.contains(answers.get(incorrectAnswers)))
                        options.add(answers.get(incorrectAnswers));
                    else --i;

                }

            }

            btn1.setText(options.get(0));
            btn2.setText(options.get(1));
            btn3.setText(options.get(2));
            btn4.setText(options.get(3));
        }

Change your if condition in chosenRiddle to chosenRiddleif条件更改为

if (answers.get(riddleCounter).equals(((Button)view).getText().toString())) {
    //Your code
} else {
    //Your Code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM