2017-03-17 65 views
1

我正在制作一个简短的问答游戏,用户输入他们的答案,然后将其存储到与包含正确答案的数组进行比较的数组中。我有两个错误,当我尝试和编译代码:从其他方法传递数组时出现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(); 
     } 
} 
+0

'tar'不会在主存... – Li357

回答

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);,你还可以创建一个变量获得德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(); 
    } 
} 
0

变化:

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

要:

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

也'correctAnswers '没有定义 – Dazak

相关问题