2013-03-02 82 views
0

我有其中的主要方法,用户输入的答案,然后在一个单独的类需要有看到,如果用户通过一种方法来告诉多少是错误的,正确的方法的驾驶执照测试程序和一个方法把错误的答案的数字放入一个数组中,然后主要方法需要打印出每个方法的结果。我的问题是与错误的问题数组有关。我相信我正确创建它,但我不能让主的方法来打印out.Thanks问题返回数组

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"); 



} 
} 
} 
+2

能否请您至少提的编程语言(和accordinly标记您的问题)? – 2013-03-02 09:42:20

+3

...并按''在这里和那里......? – 2013-03-02 09:43:52

+0

在Java编程和我不会没有其他id标记它。我有一个数组的问题,所以我把它标记为数组即时通讯显然是新的在这个我的不好 – dendoc01 2013-03-02 09:46:47

回答

0

检查以下行,因为它没有任何意义,你是分配给循环控制变量一个空数组的值。

i = questionWrong[i]; 

但是,整个算法您正在使用检查错题的数量是不对的,你应该尝试用另一个。随着你的实现,你只是讨论整个问题的一部分,你应该循环所有的问题,并得到不正确的问题的数量。

顺便说一句,我认为你是做了这么多使用的静态方法。从广义上讲是更好的使用实例方法,对于你的情况是完全有效的。在你的情况下使用实例方法,允许在构造函数中进行所有计算,或者通过构造函数调用方法,避免在代码中重复使用相同的代码(循环)。