1

I'm making a short quiz game where the user inputs their answers that are then stored into an array which is compared to an array containing the correct answers. I have two errors when I try and compile the code:

Cannot find symbol: variable tar and Cannot find symbol: variable correctAnswers

What am I doing wrong? is it something to do with variable scope? I'm very new at coding, any help would be appreciated. Code displayed below:

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();
        }
}
Fildor
  • 14,510
  • 4
  • 35
  • 67

2 Answers2

0

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

You are calling q.checkScore(tar, correctAnswers); and in that class there is defined neither tar nor correctAnswers... that's why your code fails to compile... Maybe this works:

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

Or in this part q.checkScore(tar, Quiz.correcAnswers); you can also create a variable to get de correctAnswers:

int[] correctAnswers = Quiz.correctAnswers;

and then pass it:

q.checkScore(tar, correcAnswers);

Anyway, your problem is that in the class QuizGamethere are not variables tar nor correctAnswers and that's why your code fails to compile.

Or the shortest way:

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();
    }
}
Dazak
  • 1,011
  • 2
  • 9
  • 17
0

Change:

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

To:

    q.checkScore(p.getAnswers();, correctAnswers);
Orest Savchak
  • 4,529
  • 1
  • 18
  • 27