【问题标题】:Incorrect when the answer is correct Android Quiz app答案正确时不正确 Android Quiz app
【发布时间】:2016-08-17 12:33:58
【问题描述】:

我正在尝试开发一个测验应用程序,我在其中手动编写一系列问题和答案,然后使用它们来设置单选按钮和文本视图的文本值。但是,当我尝试运行它并使用它时,在第一组问题中,它很好,然后正确答案变得不正确,反之亦然。截图如下。

当我按下正确的那个时正确的那个。

不正确的,但我按下了正确的(你知道答案)。

在我看来,第一组问题的值没有被移动。我尝试了我能想到的一切,但没有运气。当我尝试打印出当前文本时,我看到了我的 logcat 中发生了什么。这是我的 logcat 中的一个 sn-p。

04-24 01:56:10.880 4093-4093/com.example.muhammad.exquizme D/answer: Google
04-24 01:56:10.880 4093-4093/com.example.muhammad.exquizme D/correctAnswer: Google
04-24 01:56:10.885 4093-4093/com.example.muhammad.exquizme D/Is it correct?: Correct
04-24 01:56:10.894 4093-4147/com.example.muhammad.exquizme W/OpenGLRenderer: Fail to change FontRenderer cache size, it already initialized
04-24 01:56:32.322 4093-4093/com.example.muhammad.exquizme D/answer: Google
04-24 01:56:32.322 4093-4093/com.example.muhammad.exquizme D/correctAnswer: An algorithm
04-24 01:56:32.326 4093-4093/com.example.muhammad.exquizme D/Is it correct?: Incorrect

这是该部分的代码。

QuizQuestion[] questionArray; //global variable
    int randomIndex;//global variable

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_quiz);

        qIndexView = (TextView) findViewById(R.id.currentNumber);
        questionTextView = (TextView) findViewById(R.id.questionTextView);
        choiceBtnA = (RadioButton) findViewById(R.id.choiceA);
        choiceBtnB = (RadioButton) findViewById(R.id.choiceB);
        choiceBtnC = (RadioButton) findViewById(R.id.choiceC);
        choiceBtnD = (RadioButton) findViewById(R.id.choiceD);
        questionArray = new QuizQuestion[5];

        displayQuestion();


        final String a = choiceBtnA.getText().toString();
        final String b = choiceBtnB.getText().toString();
        final String c = choiceBtnC.getText().toString();
        final String d = choiceBtnD.getText().toString();

        choiceBtnA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswers(a);
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        displayQuestion();
                    }
                }, 2000);

                //displayQuestion();
            }
        });

        choiceBtnB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswers(b);
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        displayQuestion();
                    }
                }, 2000);
            }
        });

        choiceBtnC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswers(c);
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        displayQuestion();
                    }
                }, 2000);
            }
        });

        choiceBtnD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswers(d);
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        displayQuestion();
                    }
                }, 2000);
            }
        });
    }

//This is called every pressed
public void displayQuestion() {

        //Create Question list
        String theQuestion = "Android mobile operating system was released on 23rd September 2008 developed by who?";
        String[] choices = new String[]{"Google", "IBM", "Intel", "Oracle"};
        questionArray[0] = new QuizQuestion(theQuestion, choices, choices[0], "Computing History");

        theQuestion = "Who invented the programming language called 'Java'?";
        choices = new String[]{"James Gosling", "Steve Jobs", "Bill Gates", "Elon Musk"};
        questionArray[1] = new QuizQuestion(theQuestion, choices, choices[0], "Computing History");

        theQuestion = "Which of the following languages is more suited to a structured program?";
        choices = new String[]{"FORTRAN", "BASIC", "PASCAL", "None of the above"};
        questionArray[2] = new QuizQuestion(theQuestion, choices, choices[3], "Computer Fundamentals");

        theQuestion = "The brain of any computer system is";
        choices = new String[]{"Memory", "ALU", "CPU", "Control unit"};
        questionArray[3] = new QuizQuestion(theQuestion, choices, choices[2], "Computer Fundamentals");

        theQuestion = "The step-by-step instructions that solve a problem are called _____.";
        choices = new String[]{"An algorithm", "A list", "A plan", "A sequential structure"};
        questionArray[4] = new QuizQuestion(theQuestion, choices, choices[0], "System Analysis and Design");

        randomIndex = new Random().nextInt(questionArray.length);

        questionTextView.setText(questionArray[randomIndex].question);
        choiceBtnA.setText(questionArray[randomIndex].choices[0]);
        choiceBtnB.setText(questionArray[randomIndex].choices[1]);
        choiceBtnC.setText(questionArray[randomIndex].choices[2]);
        choiceBtnD.setText(questionArray[randomIndex].choices[3]);

    }

//checks answer when clicked
public boolean checkAnswers(String answer) {
        Log.d("answer", answer);
        String correctAnswer = questionArray[randomIndex].answer;
        Log.d("correctAnswer", correctAnswer);
        if (answer.equals(correctAnswer)) {
            Toast.makeText(PlayQuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
            Log.d("Is it correct?", "Correct");
            return true;
        } else {
            Toast.makeText(PlayQuizActivity.this, "Incorrect", Toast.LENGTH_SHORT).show();
            Log.d("Is it correct?", "Incorrect");
        }

        return false;
}

这就是我目前所拥有的一切。如果您需要澄清,请告诉我,因为我的英语不是那么好。提前致谢。

【问题讨论】:

  • 请说明您将String 传递到checkAnswers() 的方式和位置。
  • @MikeM 我已经为你更新了。现在看看。谢谢。
  • 点击时需要获取Buttons的当前文本。将onClick()方法中对checkAnswers()的调用改为checkAnswers(choiceBtnA.getText().toString());checkAnswers(choiceBtnB.getText().toString());等。
  • 如果这是您的答案,请将此作为您的答案。现在可以了。我什至没有意识到这对你来说是多么容易。哈哈谢谢。 :)

标签: java android arrays android-activity radio-button


【解决方案1】:

显示第一个问题后,您会将Buttons 的初始文本保存为答案,但当您转到下一个问题时,它们不会更新。

点击时需要获取Buttons的当前文本。将onClick()方法中对checkAnswers()的调用改为checkAnswers(choiceBtnA.getText().toString());checkAnswers(choiceBtnB.getText().toString());等。

或者,您可以改为从questionArray 获得答案String。例如checkAnswers(questionArray[randomIndex].choices[0]);checkAnswers(questionArray[randomIndex].choices[1]);等。

此外,您可以通过将代表Button 的索引的int 传递给checkAnswers() 来稍微缩减代码,然后在此处获取选定的答案。

choiceBtnA.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        checkAnswers(0);
        ...
    }
});

...

public boolean checkAnswers(int index) {
    String answer = questionArray[randomIndex].choices[index];
    String correctAnswer = questionArray[randomIndex].answer;
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多