答案正确时,Android测验应用程序不正确

穆罕默德·卡图比格(Muhammad Catubig)

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

当我按下正确的一个正确的。

在此处输入图片说明

不正确的一个,但是我按了正确的一个(您知道答案)。

在此处输入图片说明

It seems to me that the first set of question's values were not moved. I tried everything I can think of but no luck. I see what happens in my logcat when I tried to print out the current text. Here's a snippet from my logcat.

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

Here's the code for that part.

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

That's all I have at the moment. Let me know if you need clarification coz my english is not that good. Thanks in advance.

Mike M.

After you display the first question, you're saving the Buttons' initial text as the answers, but they're not getting updated when you move to the next question.

You need to get the Buttons' current text when clicking. Change the calls to checkAnswers() in the onClick() methods to checkAnswers(choiceBtnA.getText().toString());, checkAnswers(choiceBtnB.getText().toString());, etc.

或者,你可以得到答案String,从questionArray代替。例如,checkAnswers(questionArray[randomIndex].choices[0]);checkAnswers(questionArray[randomIndex].choices[1]);等。

此外,您可以通过将int代表Button的索引传递到来稍微减少代码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;
    ...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

答案正确时,Android测验应用程序不正确

来自分类Dev

在测验应用程序android的结果中显示正确答案

来自分类Dev

春季启动应用程序启动时不正确的应用程序名称

来自分类Dev

Android预览显示正确,但运行应用程序不正确

来自分类Dev

在发布的游戏上“应用程序配置不正确”

来自分类Dev

该应用程序格式不正确

来自分类Dev

在Laravel应用程序中请求IP不正确

来自分类Dev

GTK2应用程序的主题不正确

来自分类Dev

托盘应用程序显示不正确

来自分类Dev

某些图标主题的应用程序图标不正确

来自分类Dev

应用程序的基本 URL 不正确

来自分类Dev

具有多个正确答案的多项选择测验应用程序的设计方法

来自分类Dev

插入美元金额时,C# Windows 窗体应用程序输入字符串的格式不正确

来自分类Dev

在 android 模拟器 LibGDX 中使用应用程序时分辨率不正确

来自分类Dev

LocalDate.plus不正确的答案

来自分类Dev

除法答案执行不正确

来自分类Dev

来自功能的答案不正确

来自分类Dev

在Java线程应用程序中获取不正确的输出?

来自分类Dev

“不正确的Content-Type:”异常引发Angular MVC 6应用程序

来自分类Dev

Windows 10中Qt Desktop应用程序的不正确缩放

来自分类Dev

'/'应用程序中的服务器错误->服务器标记格式不正确

来自分类Dev

iOS应用程序在Mac iTunes中显示不正确的日期

来自分类Dev

为什么NBug HostApplicationVersion显示的应用程序版本不正确?

来自分类Dev

动态内容中的属性选择不正确-逻辑应用程序Azure

来自分类Dev

Cakephp应用程序在ec2上显示不正确的路径

来自分类Dev

Safari->“应用程序缓存清单的MIME类型不正确:文本/纯文本。”

来自分类Dev

为什么cons的嵌套应用程序会构造不正确的列表?

来自分类Dev

'/ WEBSITE'应用程序中的c#服务器错误。'='附近的语法不正确

来自分类Dev

错误:由于并行配置不正确,导致应用程序启动失败

Related 相关文章

  1. 1

    答案正确时,Android测验应用程序不正确

  2. 2

    在测验应用程序android的结果中显示正确答案

  3. 3

    春季启动应用程序启动时不正确的应用程序名称

  4. 4

    Android预览显示正确,但运行应用程序不正确

  5. 5

    在发布的游戏上“应用程序配置不正确”

  6. 6

    该应用程序格式不正确

  7. 7

    在Laravel应用程序中请求IP不正确

  8. 8

    GTK2应用程序的主题不正确

  9. 9

    托盘应用程序显示不正确

  10. 10

    某些图标主题的应用程序图标不正确

  11. 11

    应用程序的基本 URL 不正确

  12. 12

    具有多个正确答案的多项选择测验应用程序的设计方法

  13. 13

    插入美元金额时,C# Windows 窗体应用程序输入字符串的格式不正确

  14. 14

    在 android 模拟器 LibGDX 中使用应用程序时分辨率不正确

  15. 15

    LocalDate.plus不正确的答案

  16. 16

    除法答案执行不正确

  17. 17

    来自功能的答案不正确

  18. 18

    在Java线程应用程序中获取不正确的输出?

  19. 19

    “不正确的Content-Type:”异常引发Angular MVC 6应用程序

  20. 20

    Windows 10中Qt Desktop应用程序的不正确缩放

  21. 21

    '/'应用程序中的服务器错误->服务器标记格式不正确

  22. 22

    iOS应用程序在Mac iTunes中显示不正确的日期

  23. 23

    为什么NBug HostApplicationVersion显示的应用程序版本不正确?

  24. 24

    动态内容中的属性选择不正确-逻辑应用程序Azure

  25. 25

    Cakephp应用程序在ec2上显示不正确的路径

  26. 26

    Safari->“应用程序缓存清单的MIME类型不正确:文本/纯文本。”

  27. 27

    为什么cons的嵌套应用程序会构造不正确的列表?

  28. 28

    '/ WEBSITE'应用程序中的c#服务器错误。'='附近的语法不正确

  29. 29

    错误:由于并行配置不正确,导致应用程序启动失败

热门标签

归档