2017-02-11 4 views
0

2つの配列の間にどの質問が欠けているのかを私のプログラムで表示したい。私は間違って入力する最初の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 Javaプログラムでは偶然の使用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!

関連する問題