2017-02-11 1 views
0

내 프로그램에서 두 배열간에 누락 된 질문을 표시하려고합니다. 나는 처음 12에 대한 모든 정답을 입력하고 마지막 3은 잘못 입력합니다. 내가 그것을 표시하려고 할 때마다, 그것은 이상한 답 대답으로 온다, 나는 그것이 메모리 주소일지도 모른다라고 생각한다. 내가 놓친 질문 번호를 인쇄 할 수있는 모든 것을 시도했다. 제발 어떤 도움이라도 극도로 감사 할 것입니다!배열에서 누락 된 질문이 표시됩니다.

import java.util.Scanner; //import scanner 

public class DriverTestBlah { 
public static void main(String [] args){ 

Scanner input = new Scanner(System.in); 
    char[] correctAnswers = {'A','D','C','A','A','D','B', 
    'A','C','A','D','C','B','A','B'}; 
    char[] userAnswer = new char[correctAnswers.length]; 
     for(int i = 0; i < 15; i++) //print question numbers/takes user input 
     { 
     System.out.print("Question " + (i + 1) + ":"); 
      userAnswer [i] = input.nextLine().charAt(0); 
     }//end of for loop 
    for(int i = 0; i < questions.length; i++) //for loop prints you missed 
    { 
    System.out.print("You missed question: "); 
     for(int y = 0; y < 1; y++) //for loop only takes one question # at a time 
      { 
      System.out.println(" " + which_questions_missed(userAnswer, correctAnswers)); 

      }//end of question number for loop 
    }//end of 
    }//end of main 
//create new class that determines which questions missed and returns their numbers 
    public static int []which_questions_missed(char[] userAnswer, char[] correctAnswers){ 
    int missed[] = new int[correctAnswers.length]; 
     for (int i = 0; i < correctAnswers.length; i++){ 
      if (userAnswer[i] != correctAnswers[i]){ 
      missed[i]=(i+1);//chooses the index of the missed answers and adds 1 to display 
      } 
     } 
    return missed; 
    } 
}//end of class 
+0

배열을 인쇄하려고합니다. 'Arrays.toString (which_questions_missed (userAnswer, correctAnswers))'가 필요합니다. 그러나 알고리즘이 약간 이상해 보입니다. 기대하는 결과를 얻지 못할 수도 있습니다. – jackgu1988

답변

0

나는 방법 (안 클래스)이 같은 which_questions_missed을 수정합니다. 내가 whichQuestionsMissed이에 which_questions_missed에서 이름을 변경하는 방법에 주목하는 것은 snake_case 자바 프로그램에서 우연 사용 camelCase입니다 : 그것은 사용자뿐만 아니라 동등한 소문자를 입력 할 수 있습니다 Character.toUpperCase()을 활용

import java.util.Scanner; 

public class Main { 
    public static void main(String [] args){ 
    Scanner input = new Scanner(System.in); 
    char[] correctAnswers = {'A','D','C','A','A','D','B', 'A','C','A','D','C','B','A','B'}; 
    char[] userAnswer = new char[correctAnswers.length]; 
    for(int i = 0; i < 15; i++) { 
     System.out.print("Question " + (i + 1) + ":"); 
     userAnswer[i] = input.nextLine().charAt(0); 
    } 
    whichQuestionsMissed(userAnswer, correctAnswers); 
    } 

    public static void whichQuestionsMissed(char[] userAnswer, char[] correctAnswers) { 
    for(int i = 0; i < userAnswer.length; i++) { 
     if(Character.toUpperCase(userAnswer[i]) != correctAnswers[i]) { 
     System.out.println("You missed question: " + i); 
     } 
    } 
    } 
} 

.

사용해보기 here!

관련 문제