2016-04-04 10 views
0

Bir test için "doğru yanıtlar" ve diğeri kullanıcı giriş yanıtları ile biri, 2 karakterli dizi oluşturmakla görevlendirildim. Kod düzgün çalışıyor ve derliyor, ancak 10 cevap girdisini programa girdiğimde, sınır dışı özel bir dizi alıyorum. BuradaBu diziyi sınırlar istisnası dışında düzeltme

//Part 2 
    char[] correctAnswers = {'b', 'd', 'a', 'a', 'c', 'a', 'b', 'a', 'c', 'd'}; //Char arrays 
    char[] studentAnswers = new char[10]; 

    System.out.println("What are the students 10 answers?"); //Getting student answers 
    for(int i = 0; i < correctAnswers.length; i++) 
    { 
     System.out.println("What is the answer to the " + i + " question"); 
     studentAnswers = scan.next().toCharArray(); 
    } 

    int points = 0; //Used to calculate pass or fail 


    for(int i = 0; i < correctAnswers.length; i++) 
    { 
     if (correctAnswers[i] == studentAnswers[i]) 
     points++; 
    } 




    if (points >= 8) 
    { 
     System.out.println("Congratulations! \nYou have passed exam."); 
     System.out.println("Total number of correct answers: " + points); //print points 
     System.out.println("Total number of incorrect answers: " + (correctAnswers.length - points)); //10 - points would equal the remaining amount of points available which would be how many were missed. 
    } 

    else 
    { 
     System.out.println("Sorry, you have not passed the exam!"); 
     System.out.println("Total number of correct answers: " + points); 
     System.out.println("Total number of incorrect answers: " + (correctAnswers.length - points)); 
    } 
+2

Yığın iz edin. Hatayla karşılaştığınızda, tam yığın izinizi daha iyi göndermelisiniz – HungPV

+2

Sorun burada: 'studentAnswers = scan.next(). ToCharArray();', kullanıcı girdisini "studentAnswers" öğesine eklemek istiyorsanız, diziyi değiştirmeyin her girişte – Maljam

cevap

1

Sen her tekrarında bir cevabı bekliyoruz demektir bir döngüde, cevapları alıyorsanız tam bir karakter dizisini girer emin olabilirsiniz Bu şekilde, fakat Her yinelemede studentAnswers dizisinin tamamını atayabilirsiniz.

Muhtemelen her giriş hattında tek karakter cevabı bekliyoruz varsayarak

studentAnswers = scan.next().toCharArray(); 

studentAnswers[i] = scan.nextLine().charAt(0); 

olarak değişmelidir. giriş tek bir hat temin edilirse

, boşlukla ayrılmış, sen

studentAnswers[i] = scan.next().charAt(0); 

kullanabilir veya birlikte tüm döngü yerine:

studentAnswers = scan.nextLine().split(" "); 
+0

Teşekkür ederim. Bu mükemmel bir anlam ifade ediyor. – Tyler

1

sorundur, studentAnswers = scan.next().toCharArray(); Eğer kullanıcıdan tam olarak 10 karakter uzunluğunda bir yanıt aldığınızdan emin olmalıyız: Burada

kod parçacığı olduğunu.

Bunu yapmak için böyle bir şey yapabilirsiniz.

while(true){ 
    char[] temp=scan.next().toCharArray(); 
    if(temp.length==10){ 
     studentAnswers=temp; 
     break; 
    } 
    else{ 
     //print that the length is incorrect. 
    } 
} 

Eğer kullanıcı uzunluğu 10.