Je réalise un jeu-questionnaire court dans lequel l'utilisateur entre ses réponses qui sont ensuite stockées dans un tableau qui est comparé à un tableau contenant les bonnes réponses. J'ai deux erreurs quand je tente de compiler le code:Erreur Java lors du passage de tableaux à partir d'autres méthodes
Impossible de trouver le symbole: le goudron variable et Vous ne trouvez pas le symbole: correctAnswers variables
Qu'est-ce que je fais mal? est-ce quelque chose à voir avec la portée variable? Je suis très nouveau au codage, toute aide serait appréciée. Code affiché ci-dessous:
package quizgame;
import java.util.Arrays;
import java.util.Scanner;
class player {
Scanner Keyboard = new Scanner(System.in);
int playerResponse = 0;
int[] tar;
public int[] getAnswers(){
System.out.println("Enter answers");
int[] tar = new int [3];
for (int i = 0; i < tar.length; i++) {
tar[i] = Keyboard.nextInt();
}
return tar;
}
}
class Quiz {
int playerScore;
static final int [] correctAnswers = {2,3,2};
String[] questionOneAnswers = {"1: Pb", "2: Au", "3: Mg"};
String[] questionTwoAnswers = {"1: Dollar", "2: Rubbee", "3: Cedi"};
String[] questionThreeAnswers = {"1: 1886", "2: 1765", "3: 1775"};
public void gameStart() {
System.out.println("Are you ready for a quiz?");
System.out.println("Question 1: What is the symbol for gold?");
System.out.println(Arrays.toString(questionOneAnswers));
System.out.println("Question 2: What is the currency of Ghana?");
System.out.println(Arrays.toString(questionTwoAnswers));
System.out.println("Question 3: When did the American "
+ "revolution start?");
System.out.println(Arrays.toString(questionThreeAnswers));
}
public int checkScore(int[] tar, int[]correctAnswers){
for (int i = 0; i <tar.length-1; i++){
if(tar[i] == correctAnswers[i]){
playerScore ++;
}
}
return playerScore;
}
public void ShowScore() {
System.out.println("Your score is " + playerScore);
}
}
public class QuizGame {
public static void main(String[] args) {
Scanner Keyboard = new Scanner(System.in);
Quiz q = new Quiz();
player p = new player();
q.gameStart();
p.getAnswers();
q.checkScore(tar, correctAnswers);
q.ShowScore();
}
}
'tar' n'existe pas dans la principale ... – Li357