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));
}
Yığın iz edin. Hatayla karşılaştığınızda, tam yığın izinizi daha iyi göndermelisiniz – HungPV
Sorun burada: 'studentAnswers = scan.next(). ToCharArray();', kullanıcı girdisini "studentAnswers" öğesine eklemek istiyorsanız, diziyi değiştirmeyin her girişte – Maljam