问题返回数组

时间:2013-03-02 09:38:58

标签: java arrays

我有一个驾驶执照测试程序,在主方法中用户输入答案然后在一个单独的类中需要有一个方法来查看用户是否通过了一个方法来判断有多少是错误的和正确的方法将错误答案的数字放入数组中,然后主方法需要打印出每个方法的结果。我的问题是问题数字的数组是错误的。我相信我正确地创建了它但我无法获得打印它的主要方法。谢谢

   public class DriverExam
   {
      static String[] correctAnswers={"b","d","a","a","c","a","b","a","c","d","b","c","d","a","d","c","c","b","d","a"};
      static String[] userAnswers=new String[20];


   //constructer
      public  DriverExam(String[] user)
      {
         userAnswers=user;
      }




   //method to see if you passed
      public static boolean passed()
      {
         boolean pass=false;
         int correctCount=0;
         int incorrectCount=0;
         for(int i=0;i<userAnswers.length;i++)
         {
            if(userAnswers[i].equals(correctAnswers[i]))
            {
               correctCount++;
            }
            else
            {
               incorrectCount++;
            }
         }

         if(correctCount>14)
         {pass=true;}
         return pass;

      }

      //method to find number correct
      public static int totalCorrect()
      {
         boolean pass=false;
         int correctCount=0;
         int incorrectCount=0;
         for(int i=0;i<userAnswers.length;i++)
         {
            if(userAnswers[i].equals(correctAnswers[i]))
            {
               correctCount++;
            }
            else
            {
               incorrectCount++;
            }
         }

         return correctCount;

      }

      //method to tell how many were not correct
      public static int totalIncorrect()
      {
         boolean pass=false;
         int correctCount=0;
         int incorrectCount=0;
         for(int i=0;i<userAnswers.length;i++)
         {
            if(userAnswers[i].equals(correctAnswers[i]))
            {
               correctCount++;
            }
            else
            {
               incorrectCount++;
            }
         }

         return incorrectCount;

      }

      public static int[] questionsMissed()
      {

         boolean pass=false;
         int correctCount=0;
         int incorrectCount=0;
         for(int i=0;i<userAnswers.length;i++)
         {
            if(userAnswers[i].equals(correctAnswers[i]))
            {
               correctCount++;
            }
            else
            {
               incorrectCount++;
            }
         }
      int[] questionWrong=new int[incorrectCount];

      for(int i=0;i<questionWrong.length;i++)
      {
      if(!userAnswers[i].equals(correctAnswers[i]))
      {i=questionWrong[i];
      }
      }
      return questionWrong;
      }
      }

import java.util.Scanner;
public class DriverExamDemo
{
public static void main(String[] args)
{
String[] userAnswers=new String[20];
String answer;

System.out.println("please enter the testee's answers as the correspond with the question number");

for(int i=0;i<userAnswers.length;i++)
{
Scanner input=new Scanner(System.in);
System.out.println("Q."+(i+1));
answer=input.next();
userAnswers[i]=answer;
}
// constructor to send the users answers
DriverExam exam1 = new DriverExam(userAnswers);

//Check passed boolean to see if the user passed or failed
if(DriverExam.passed())
System.out.println("you passed your driving exam");
else
System.out.println("you failed your driving exam");

//check how many answers were correct and incorrect
System.out.println("you answered "+DriverExam.totalCorrect()+" questions correctly");
System.out.println("you answered "+DriverExam.totalIncorrect()+" questions incorrectly");

int[] questionsWrong=DriverExam.questionsMissed();
for (int i=0;i<questionsWrong.length;i++)
{
System.out.println("you got question " +questionsWrong[i]+"incorrect");



}
}
}

1 个答案:

答案 0 :(得分:0)

检查以下行是因为没有意义,您要为循环控制变量分配空数组的值。

i = questionWrong[i];

但是,你用来检查错误问题的数字的整个算法是错误的,你应该尝试另一个。有了你的实现,你只是在整个问题的一个子集上,你应该遍历所有的问题,并获得不正确的数字。

顺便说一句,我认为你正在使用静态方法。从广义上讲,更好地使用实例方法,并且对于您的情况是完全有效的。在你的情况下使用实例方法,允许在构造函数中进行所有计算,或者由构造函数调用方法,并避免在整个代码中复制相同的代码(循环)。