【问题标题】:Compiler errors due to missing variables (the variable is defined in a different class)由于缺少变量而导致的编译器错误(变量在不同的类中定义)
【发布时间】:2012-05-19 04:47:12
【问题描述】:

我是 Java 编程的初学者,正在尝试制作剪刀石头布游戏。 对于缺少 cmets,我深表歉意。

这是我的主程序,它调用其他两个对象。

 import java.util.*;

  public class RPSMain extends RPSPlayer{
   RPSPlayer player = new RPSPlayer();
   RPSGame gameObject = new RPSGame ();
   public void main () {


     Random generator = new Random ();
     Scanner sc = new Scanner(System.in);
     System.out.print ("Number of Rounds: ");
     int rounds = sc.nextInt();


    //Call and process all of the methods found in RPSPlayer and RPSGame
     for (int i = 0; i < rounds; i++){
        player.makeThrow();
        gameObject.makeThrow();
        gameObject.announceWinner (compThrow, getPThrow);
     }
    //Final Output
     System.out.print (gameObject.bigWinner(winner, rounds));
  }
//accessor to return round to RPSGame
   public static int getRound (int round){
     this.round = round;
     return round;
  }
}

我的第一个对象是玩家输入他们想要的投掷次数,并在那里进行处理。

  import java.util.*;

  public class RPSPlayer {

  public static void main (String args[]) {
     Random generator = new Random ();
     Scanner sc = new Scanner(System.in);
  }
//This method gets the throw, and loops if throw is not within 1 and 3
  public static int makeThrow (){
     Scanner sc = new Scanner (System.in);
     int playerThrow;
     do{
        System.out.print ("Enter your throw (1=Rock, 2=Paper, 3=Scissors)");
        playerThrow = sc.nextInt();
     } while (playerThrow > 3 && playerThrow < 1);
     return playerThrow;
  }

    //Accessor method
  public static int getThrow (int playerThrow){
    this.playerThrow = playerThrow;
     return playerThrow;
  }


 }

最后一个对象是所有计算发生的地方。 有很多变量无法正确编译,我也不太明白为什么......

  import java.util.*;

 public class RPSGame extends RPSPlayer{
  RPSPlayer player = new RPSPlayer();
  RPSGame game = new RPSGame ();
  RPSMain mainRPS = new mainRPS();
   public static void main (String args[]) {

     Random generator = new Random ();
     Scanner sc = new Scanner(System.in);
     int rounds = mainRPS.getRound(rounds);
   }
   //Random Throw Generator
   public static int makeCompThrow (){
     int Max = 3;
     int Min = 1;

     int compThrow =   Min + (int)(Math.random() * ((Max - Min) + 1));
     return compThrow;
  }

   //  Get the throw from the player in RPSPlayer
       public static int getPlayerThrow (){
     RPSPlayer player = new RPSPlayer();
     int getPThrow = player.makeThrow();
     return getPThrow;
  }

 //Does all of the calculatoins and ouputs who threw what.
   public static int announceWinner (int compThrow, int getPThrow) {
     int winner = 0;

     if (getPThrow == 1){
        System.out.println ("Player throws ROCK.");
     }
     else if (getPThrow == 2){
        System.out.println ("Player throws PAPER.");
     }
     else if (getPThrow == 3){
        System.out.println ("Player throws SCISSORS.");
     }


     if (compThrow == 1){
        System.out.println ("Computer throws ROCK.");
     }
     else if (compThrow == 2){
        System.out.println ("Computer throws PAPER.");
     }
     else if (compThrow == 3){
        System.out.println ("Computer throws SCISSORS.");
     }

     if (getPThrow == compThrow){
        winner = 3;
     }
     else if (getPThrow == 1 && compThrow == 3){
        winner = 1;
     }
     else if (getPThrow == 1 && compThrow == 2){
        winner = 2;
     }
     else if (getPThrow == 2 && compThrow == 1){
        winner = 1;
     }
     else if (getPThrow == 2 && compThrow == 3){
        winner = 2;
     }
     else if (getPThrow == 3 && compThrow == 1){
        winner = 2;
     }
     else if (getPThrow == 3 && compThrow == 2){
        winner = 1;
     }  

     return winner;
   }

//Final Output with imported values of 'rounds' and 'winner'
   public int bigWinner (int winner, int rounds){
     int tie = 0;
     int playerWins = 0;
     int compWins = 0;

     if (winner == 1){
        playerWins = playerWins + 1;
     }

     else if (winner == 0){
        tie = tie + 1;
     }

     else if (winner == 3){
        compWins = compWins + 1;
     }
     System.out.println ("You win " +playerWins+ ". Computer wins " +(compWins)+ ".");
     if (playerWins > compWins){
        System.out.print ("You win!"); 
     }
     if (playerWins < compWins){
        System.out.print ("Computer wins!"); 
     }

     if (playerWins == compWins){
        System.out.print ("It's a tie!"); 
     }
     return tie;
  }


 }

再次编译时,加入WATTO Studios的建议后出现2个新错误,这些是编译错误:

RPSMain.java:23: cannot find symbol
symbol  : variable winner
location: class RPSMain
     RPSGame.bigWinner(winner, rounds);
                       ^
RPSMain.java:23: non-static method bigWinner(int,int) cannot be referenced 
from a static context
     RPSGame.bigWinner(winner, rounds);

如果我从 RPSGame 引用它为什么找不到变量“赢家”,为什么它仍在 RPSMain 中搜索变量?

【问题讨论】:

  • 能否请您发布您收到的第一条错误消息。您总是首先修复第一个错误,因为它通常会在以后产生错误。
  • "有很多变量编译不正确,我也搞不明白为什么。"什么是编译错误?
  • RPSMain mainRPS = new mainRPS(); 应该是RPSMain mainRPS = new RPSMain();
  • 我在我的回答中添加了更多信息,这将帮助您更清楚地理解。

标签: java compilation compiler-errors scope


【解决方案1】:

对于这些错误,在您的 RPSMain 类中,您正尝试从其他类中访问变量。你的代码在这里...

 for (int i = 0; i < rounds; i++){
    player.makeThrow();
    gameObject.makeThrow();
    gameObject.announceWinner (compThrow, getPThrow);
 }

其实应该是这样的……

 for (int i = 0; i < rounds; i++){
    int playerThrow = player.makeThrow();
    int compThrow = gameObject.makeCompThrow();
    gameObject.announceWinner (compThrow, playerThrow );
 }

请注意,当我们调用像makeThrow() 这样的方法时,我们会捕获变量并将其命名为playerThrow。现在我们可以在announceWinner() 方法中使用这个变量。 compThrow 变量也是如此。

您在 RPSGame.bigWinner(winner, rounds); 行中做同样的事情 - 它抱怨 winner 不存在。这是真的——winner 不是RPSMain 中的变量,它只是RPSGame 中的一个变量——你不能像这样在不同的类之间共享变量。

您的gameObject.announceWinner() 方法返回代表实际获胜者的int。如果要使用此返回值,则需要将其捕获到变量中。目前您在RPGMain...中有这样的代码...

for (int i = 0; i < rounds; i++){
   int playerThrow = player.makeThrow();
   int compThrow = gameObject.makeCompThrow();
   gameObject.announceWinner (compThrow, playerThrow );
}
System.out.print (gameObject.bigWinner(winner, rounds));

如果你想保留announceWinner()方法返回的int,你必须通过如下调整来捕获它...

for (int i = 0; i < rounds; i++){
   int playerThrow = player.makeThrow();
   int compThrow = gameObject.makeCompThrow();
   int winner = gameObject.announceWinner (compThrow, playerThrow );
   System.out.print (gameObject.bigWinner(winner, rounds));
}

现在表示从gameObject.announceWinner() 返回的值将存储在RPGMain 类中名为winner 的局部变量中。现在当它尝试在下一行的gameObject.bigWinner() 方法中使用winner 变量时,它就知道了该值。

要修复您的non-static method bigWinner(int,int) cannot be referenced from a static context 错误,您需要更改以下行...

public int bigWinner (int winner, int rounds){

要有static这个词,像这样...

public static int bigWinner (int winner, int rounds){

或者,更好的是,从代码中的任何位置删除单词static。如果您是 Java 新手,尝试使用 static 变量和方法只会让事情变得更复杂,而它们确实需要,而且我看不出您需要它们成为 static 的任何理由你的程序。

【讨论】:

  • @kryyn:进行这些更改后重新编译并重新发布您遇到的任何错误!
【解决方案2】:

compThrowgetPThrowRPSGame 的局部变量。你不能在RPSMain 中使用它们。 使用它们的一种方法是通过方法调用将它们作为参数发送到RPSMain,并在RPSMain 中重新声明变量以接受这些。

其他更可取的解决方案是将它们设置为类RPSPlayer 中的受保护实例变量。这样,RPSGameRPSMain 都可以通过继承获得它们。

【讨论】:

    【解决方案3】:

    注意:您似乎拥有大量的静态方法,以及一些完全由静态方法组成的类,但您仍然实例化该类并调用这些方法,就好像它们是对象实例的一部分一样.

    一般来说,调用静态方法时,应该使用静态引用:

    RSPGame.makeCompThrow();
    

    而不是这个

    RSPGame game = new RSPGame().
    game.makeCompThrow();
    

    应该生成编译器警告。

    【讨论】:

      【解决方案4】:

      我会挑剔你的makeThrow 方法:

      } while (playerThrow > 3 && playerThrow < 1);
      

      这永远不会循环,因为没有数字小于 1 并且 (&amp;&amp;) 大于 3。 它应该是

      } while (playerThrow > 3 || playerThrow < 1);
      

      大于 3 或 (||) 小于 1。现在它应该可以按预期工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-06
        • 1970-01-01
        • 2021-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-19
        • 1970-01-01
        相关资源
        最近更新 更多