java - 由于缺少变量导致的编译器错误(变量在不同的类中定义)

标签 java compilation compiler-errors scope

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

这是我的主程序,它调用了另外两个对象。

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

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

  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 引用,为什么它找不到变量“winner”,为什么它仍在搜索 RPSMain 以查找变量?

最佳答案

对于这些错误,在您的 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

关于java - 由于缺少变量导致的编译器错误(变量在不同的类中定义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10662377/

相关文章:

java - 将 Jpanel 写入缓冲图像堆栈溢出

java - 连接 Esper 和 PostgreSQL

compiler-errors - 如何在Travis CI容器中安装具有webp支持的较新imagemagick?

linux - 如何 : Conditional include of header file

c++ - 如何在 Windows 上制作、制作和编译 C++14

qt - 在Windows中编译OpenCV SVN干线-highgui/libVideoInput中的错误

ios - Amazon IOS SDK : compiler warnings - ARC issue?

java - 如何使用 ANT Build 排除特定的 java 文件夹和 java 文件

java - 包裹在 JPanel 内的 JScrollPane 内的 JTable 无法捕获 KeyEvent

java - 如何在 Netbeans 中使用 -g 选项进行编译?