Java 从其他方法传递数组时出错

Java error when passing arrays from other methods

我正在制作一个简短的问答游戏,用户输入他们的答案,然后将其存储到一个数组中,该数组与包含正确答案的数组进行比较。当我尝试编译代码时出现两个错误:

找不到符号:变量 tar 和 找不到符号:变量 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();
        }
}

在你的主 class:

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();
    }
}

您正在调用 q.checkScore(tar, correctAnswers); 并且在那个 class 中既没有定义 tar 也没有定义 correctAnswers...这就是您的代码无法编译的原因...也许这有效:

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();
        tar = p.getAnswers();//Assign the int[] returned by p.getAns
        q.checkScore(tar, Quiz.correcAnswers); // get the static array correctAnswers and pass it as an argument of the method
        q.ShowScore();
    }
}

或者在这部分q.checkScore(tar, Quiz.correcAnswers);你也可以创建一个变量来获得正确的答案:

int[] correctAnswers = Quiz.correctAnswers;

然后传过去:

q.checkScore(tar, correcAnswers);

无论如何,您的问题是 class QuizGame 中既没有变量 tar 也没有 correctAnswers,这就是您的代码无法编译的原因。

或最短路线:

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();
        q.checkScore(p.getAnswers(), Quiz.correcAnswers); //Pass directly the arrays returned by the calls p.getAnswers() and Quiz.correcAnswers without assign that returns to variables
        q.ShowScore();
    }
}

变化:

    p.getAnswers();
    q.checkScore(tar, correctAnswers);

收件人:

    q.checkScore(p.getAnswers();, correctAnswers);