2016-11-23 108 views
-1

我正在尝试创建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,并且每个问题的人总是会选择正确答案一次,因为继续按钮不会直到那时才会出现。所以如果第一次选错了答案,它不应该增加正确的计数器。

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

+2

每当您调用checkAnswer()时,'correctcount'和'wrongcount'的值将被设置为0 – User528491

+0

假设每个问题的意图是查看每个问题是否至少有一个错误答案,则应该使用标志而不是计数器。如果你有更多的问题,在性能方面,开关盒比if ... else要好。它不是检查哪个案例得到满足,而只是决定哪个案件需要执行。 –

+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。

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

+0

对不起,我仍然困惑。所以,如果我改变这样的代码 - – aa4931

+0

告诉我们你所得到的。编辑您的帖子以帮助他人。 – msagala25

+0

我编辑了我的帖子。试试这个。 – msagala25

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

} 

它是一个很好的做法,使用开关而不是if ... else如果匹配一个变量与多个值。

+0

那工作.....你是一个天才......非常感谢你。上帝祝福你 :) – aa4931