10 soru testine doğru cevaplarla dolu bir char dizisine sahip bir program yapmakla görevlendirildim. Kullanıcıların cevapları ile doldurulmuş bir dizi yaptım ve kullanıcı cevaplarını test cevaplarıyla kontrol etmesi ve kullanıcı girdisi verildikten sonra bir çıkış ya da başarısızlık vermesi gerekiyordu. Kod derler ve 10 karaktere girebilirim, ancak ne karakterleri girer girsem de, çıktı her zaman 10 cevap yanlış 10'dur.Char dizisi doğru bir şekilde okuma okumıyor
Son birkaç saat boyunca bunu anlamaya çalışıyorum. ve burada biraz yardım etmeyi umuyordu.
//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++)
studentAnswers = scan.next().toCharArray();
int points = 0; //Used to calculate pass or fail
for (int i = 0; i < correctAnswers.length; i++);
int j = 0; //Used to identify each element in the array
if (correctAnswers[j] == studentAnswers[j]) //Checks each answer with the correct answers and adds 1 point if it is true
{
points++;
j++;
}
else {
j++;
}
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));
}
Teşekkür ederim bu mantıklı, ancak şimdi çıktıda sınırlar dışı bir dizi alıyorum? – Tyler
@Tyler sadece 10 karakterden daha az veya daha fazla girdiğinizde tahmin ediyorum. – maraca
Tam olarak 10 giriyorum, bu dizinin boyutu o zaman bu hatayı alıyorum. Ben hiç karşılaşmadığım bu hatayı düzeltmek için çok karışıkım. – Tyler