2017-05-01 3 views
1

In diesem Programm soll ich zurückgeben, wie viele Fragen ich richtig oder falsch habe. Aber unabhängig davon, was ich ablege, wird es sagen, dass ich 20 Fragen richtig und 0 falsch habe. Wer weiß, wie man das repariert, damit es genauer ist?Warum sagt es, dass ich nichts falsch mache?

Klasse:

public class KNW_DriverExam 
{ 
    //Create the arrays/Declare variable 
    //Intialize theAnswers array 
    private String[] theAnswers = {"B" , "D" , "A" , "A" , "C" , 
           "A" , "B" , "A" , "C" , "D" , 
           "B" , "C" , "D" , "A" , "D" , 
           "C" , "C" , "B" , "D" , "A" }; 
    private String[] userAnswers; 
    int[] missed = new int [theAnswers.length]; 

    /**The DriverExam method, recieves answers 
    * @param Answer, the answer 
    * */ 
    public KNW_DriverExam(String[] Answer) 
    { 
    userAnswers = new String[theAnswers.length]; 

    for(int i = 0; i < theAnswers.length; i++) 
    { 
     userAnswers[i] = theAnswers[i]; 
    } 
    } 

    /**The passed method, see if user passes or fails 
    * @return true if user passed 
    * @return false if user failed 
    * */ 
    public boolean passed() 
    { 
    if(totalCorrect()>=15) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
    } 

    /**The totalCorrect method, see how many user got right 
    * @return correctCount, how many the user got right 
    * */ 
    public int totalCorrect() 
    { 
    int correctCount = 0; 

    for(int i = 0; i < theAnswers.length; i++) 
    { 
     if(userAnswers[i].equalsIgnoreCase(theAnswers[i])) 
     { 
     correctCount++; 
     } 
    } 
    return correctCount; 
    } 

    /**The totalIncorrect method, how many the user got wrong 
    * @return incorrectCount, how many the user got wrong 
    * */ 
    public int totalIncorrect() 
    { 
    int incorrectCount = 0; 

    for(int i = 0; i < theAnswers.length; i++) 
    { 
     if(!(userAnswers[i].equalsIgnoreCase(theAnswers[i]))) 
     { 
     missed[incorrectCount] = i; 
     incorrectCount++; 
     } 
    } 
    return incorrectCount; 
    } 

    /**The missedQuestions method, how many quetions user missed. 
    * @return missed, missed questions 
    * */ 
    public int[] questionsMissed() 
    { 
    return missed; 
    } 
} 

Demo:

import java.util.Scanner; 

public class KNW_DriverExamDemo 
{ 
    public static void main(String[] args) 
    { 
    Scanner input = new Scanner(System.in); 

    System.out.println("Driver's Exam/n"); 
    System.out.println("20 Multiple Choice Questions Mark A,B,C,D"); 

    //Inputting string 
    String[] answers = new String[20]; 
    String answer; 

    for(int i = 0; i < 20; i++) 
    { 
     do 
     { 
     System.out.println((i + 1) + ": "); 
     answer = input.nextLine(); 
     } 
     while(!isValidAnswer(answer)); 
     { 

     answers[i] = answer; 
     } 
    } 

    KNW_DriverExam exam = new KNW_DriverExam(answers); 

    System.out.println("Results\n\n"); 

    System.out.println("Total Correct: " + exam.totalCorrect() + "\n"); 
    System.out.println("Total Incorrect: " + exam.totalIncorrect() + "\n"); 

    if(exam.totalIncorrect() > 0) 
    { 
     System.out.println("The Incorrect Answers Are: "); 
     int missedIndex; 

     for(int i = 0; i < exam.totalIncorrect(); i++) 
     { 
     missedIndex = exam.questionsMissed()[i] + 1; 
     System.out.println(" " + missedIndex); 
     } 
    } 
    } 

    public static boolean isValidAnswer(String answer) 
    { 
    return "A".equalsIgnoreCase(answer) || 
     "B".equalsIgnoreCase(answer) || 
     "C".equalsIgnoreCase(answer) || 
     "D".equalsIgnoreCase(answer); 
    } 

}

Antwort

5

einen Blick auf Ihre Konstruktor nehmen. Wenn Sie userAnswers zuweisen, verwenden Sie theAnswers und nicht die mitgelieferte Answer.

public KNW_DriverExam(String[] Answer) { 
    userAnswers = new String[Answers.length]; 

    for(int i = 0; i < Answers.length; i++) { 
     userAnswers[i] = Answers[i]; 
    } 
} 
+0

Ah: Kopieren der * richtigen * Antworten in 'userAnswers' anstelle der * tatsächlichen * Antworten. Das scheint 0 falsch zu erklären. –

Verwandte Themen