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

时间:2017-03-17 11:55:50

标签: java arrays compiler-errors cannot-find-symbol

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

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

2 个答案:

答案 0 :(得分:0)

在你的主要课程中:

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);并且在该类中既没有定义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);中你也可以创建一个变量来获得de correctAnswers:

int[] correctAnswers = Quiz.correctAnswers;

然后通过它:

q.checkScore(tar, correcAnswers);

无论如何,你的问题是在课程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();
    }
}

答案 1 :(得分:0)

变化:

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

要:

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