我正在制作一个简短的问答游戏,用户输入他们的答案,然后将其存储到与包含正确答案的数组进行比较的数组中。我有两个错误,当我尝试和编译代码:从其他方法传递数组时出现Java错误
找不到符号:变量焦油和 找不到符号:变量correctAnswers
我在做什么错?这与可变范围有关吗?编码方面我很新,任何帮助将不胜感激。下面的代码显示:
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'不会在主存... – Li357