算错了答案

时间:2016-11-23 02:47:36

标签: java

我正在尝试创建java应用程序。 它会提出问题并为每个问题提供4种选择。

我想计算用户选择的总的正确和错误答案。

我正在使用这样的功能 -

public void checkAnswer(String choice)
{
    int correctcount=0;
    int wrongcount=0;
    if(counter==1) {
        if (choice.equalsIgnoreCase("a")) {
            correctAnswerDisplay();
            correctcount = correct count + 1;
        } else {
            wrongAnswerDisplay();
            wrongcount = wroungcount + 1;
        }
    }
    else if(counter==2) {
        if (choice.equalsIgnoreCase("d")) {
            correctAnswerDisplay();
        } else {
            wrongAnswerDisplay();
        }
}

基本上我为每个问题编写了硬编码的答案。 例如 - 对于这个问题,正确答案是" A"。所以我说如果它是" a"调用正确的答案函数,否则问题#1中的错误答案。计数器变量是显示下一个问题

如何计算所有问题的总对错答案。如果他们回答错误,我已允许用户选择其他选项,因此他们不会进入下一个问题,直到他们选择正确的答案。但每次当他们选择错误的答案计数器时会添加更多的数字,它应该只允许1次添加并停在那里。

另外,让我说我选择了错误的答案,我没有得到下一个问题,直到我按下继续按钮,并且只有当我选择正确答案时才会出现继续按钮。因此,如果我选择错误的答案2次,计数器将是2.它应该工作,因为我选择错误答案甚至三次,总错误答案计数应该只有1.并且人将总是为每个问题选择正确答案一次,因为继续按钮没有& #39; t出现在那之前。因此,如果首先选择了错误答案,则不应增加正确的计数器。

对不起,我是一名新程序员。请帮忙。

2 个答案:

答案 0 :(得分:0)

package org.test.main;

import java.util.Scanner;

public class TestMain {

    public int correctcount = 0;
    public int wrongcount = 0;
    public int counter = 1; // lets assume you have counter as 1 to start with

    public void checkAnswer(String choice) {
        if (counter == 1) {
            if (choice.equalsIgnoreCase("a")) {
                correctAnswerDisplay();
                correctcount = correctcount + 1;
            } else {
                wrongAnswerDisplay();
                wrongcount = wrongcount + 1;
            }
        } else if (counter == 2) {
            if (choice.equalsIgnoreCase("d")) {
                correctAnswerDisplay();
                correctcount = correctcount + 1;
            } else {
                wrongAnswerDisplay();
                wrongcount = wrongcount + 1;
            }
        }
    }

    private void wrongAnswerDisplay() {
        System.out.println("Wrong");

    }

    private void correctAnswerDisplay() {
        System.out.println("Correct");
    }

    public static void main(String[] args) {

        TestMain testM = new TestMain();
        Scanner scanNext = new Scanner(System.in);
        for(int i = 0; i < 2;i++) {
            System.out.print("Question#"+(i+1)+": ");
            String ans = scanNext.nextLine();
            testM.checkAnswer(ans);
            testM.counter++;
        }

        System.out.println("Number of Correct Answer:" + testM.correctcount);
        System.out.println("Number of Wrong Answer:" + testM.wrongcount);

    }
}

将您的correctcountwrongcount设为实例变量。因为每次调用方法checkAnswer()时,将在您在方法中创建的2个变量中分配0。

我试图模拟你的逻辑。试试吧。问题和答案的数量是硬编码的。所以在你的逻辑中改变它是动态的。我编写此代码以了解增量的工作原理。

答案 1 :(得分:0)

您开始使用Stackoverflow等社区是一件非常好的事情。

形成问题很明显,只有选择了正确的答案才会启用继续按钮。 (然后没有必要计算正确的答案,因为使用这样的按钮,最后你将正确地回答所有问题。无论如何,如果你真的想要这个逻辑工作,请尝试以下。)

让我们创建一个方法incrementWrongCount()来增加变量wrongcount。

public class AnswerTester{
    int correctcount = 0;
    int wrongcount = 0;

    /*Following variable indicates whether you have selected any wrong answers before
    selecting the right answer. Obviously its default value is false since you 
    have selected no wrong answers (in fact no answers) when you first
    you first answers a question*/
    boolean wronganswerselected = false; 

    public void checkAnswer(int counter, String choice){
       if(counter==1) {
            if (choice.equalsIgnoreCase("a")) {
                correctAnswerDisplay();
                correctcount = correctcount + 1;
                incrementWrongCount();
                wronganswerselected = false;//For the next question
                enableContinueButton();
            } else {
                wrongAnswerDisplay();
                wronganswerselected = true;//just indicate that you have selected a wrong answer. We will increment wrong count in incrementWrongCount()
                disableContinueButton();
            }
        } else if(counter==2) {
            if (choice.equalsIgnoreCase("d")) {
                correctAnswerDisplay();
                correctcount = correctcount + 1;
                incrementWrongCount();
                wronganswerselected = false;
                enableContinueButton();
            } else {
                wrongAnswerDisplay();
                wronganswerselected = true;
                disableContinueButton();
            }
        }
    }

    private void incrementWrongCount(){
        if(wronganswerselected){//Should increment wrongcount only if the user have selected atleast one wrong answer.
            wrongcount = wrongcount + 1;
        }
    }

    private void correctAnswerDisplay(){
        System.out.println("Correct..");
    }

    private void wrongAnswerDisplay(){
        System.out.println("Wrong..");
    }

    public int getCorrectCount(){
        return correctcount;
    }

    public int getWrongCount(){
        return wrongcount;
    }

}

这是一个很好的做法,使用switch而不是if ... else if将变量与多个值匹配。