2013-01-19 44 views
-1

对于我介绍Java的最后一个项目,我决定做一个Mastermind Peg Game。netbeans中的“找不到符号”错误(Java Netbeans 6.5)

这是提交按钮代码:

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) { 

    Integer guess1, guess2, guess3, guess4; 
    Integer rightnumber = 0, rightposition = 0; 

    guess1 = Integer.parseInt (firstInput.getText()); 
    guess2 = Integer.parseInt (secondInput.getText()); 
    guess3 = Integer.parseInt (thirdInput.getText()); 
    guess4 = Integer.parseInt (fourthInput.getText()); 

    //Values are compared to the actual guess. 
    //(THIS IS WHERE I GET THE FOLLOWING ERROR: 
    //"cannot find symbol, symbol : variable answerdigit, 
    //location: class finalproject.Singleplayer" 

    if (guess1 == answerdigit[0]); 
    { 
     rightposition = rightposition + 1; 
    } 
} 

这是启动按钮。这里生成4位数的答案/代码。

private void startButtonActionPerformed(java.awt.event.ActionEvent evt) 
{            
    // Declare variables for 4 digit answer, guess for each number 
    Integer one, two, three, four; 

    //Generate random number between 1 and 6 for each digit in the answer 
    int[] answerdigit = new int[4]; 

    for(int i=0;i<4;i++) 
    { 
     answerdigit[i]=(int)(Math.random()*6+1); 
    } 
}    

我得到一个错误:

cannot find symbol, symbol : variable answerdigit, location: class finalproject.Singleplayer 

我不明白是什么错误意味着。

+0

您使用的变量,它不是方法的范围内存在submitButtonActionPerformed(...)。阅读Java变量范围,了解错误的含义:http://www.java-made-easy.com/variable-scope.html –

回答

1

你看起来有一个变量作用域问题:answerdigit被声明为startButtonActionPerformed方法的局部变量,因此只能在这个方法内部可见,并且根本不存在于其他地方。如果你想在类的其他地方使用这个变量,那么数组变量answerdigit必须在类中声明。

+0

感谢您的回复和建议。我会尝试在课堂上宣布它,看看我是否提出了一个解决方案。 (稍后可能会回来,以后会出现问题*不寒而栗*) –

+0

在发布更多问题之前,请仔细阅读FAQ。 http://stackoverflow.com/faq –

+0

会做什么,我现在知道对所有问题进行彻底搜索是不够的。 –

2

answerdigit是无法访问的,因为你已经宣布它的地方&也仅仅是局部范围访问,为accessising任何其他地方,你必须声明它在类

例如

class cls 
{ 
int[] answerdigit; 
//your remaining code 
} 

您在

private void startButtonActionPerformed(java.awt.event.ActionEvent evt) 

宣布它和

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) 

这就是为什么它是给错误访问它。

+1

哦,是啊!我是天才! jk jk,会花上几个小时没有你的家伙的宝贵意见。 –

0

int[] answerdigit = new int[4];应该在该范围内的类不在private void startButtonActionPerformed(java.awt.event.ActionEvent evt)这个方法的范围内!

只要把int[] answerdigit = new int[4];这个说法出来的方法和你的代码将被罚款的... :)

+0

我相信我已经解决了这个问题。对于所有答案,非常感谢! –