岩纸剪刀编译器错误

时间:2015-02-15 21:19:52

标签: java object

我正在尝试构建一个将多个课程结合在一起的石头剪刀游戏。我在面向对象编程方面遇到了很多困难,我无法弄清楚如何纠正我的编译器错误。以下是我的代码:

主要方法:

 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++){
   int playerThrow = player.makeThrow();
   int compThrow = gameObject.makeCompThrow();
   int winner = gameObject.announceWinner (compThrow, playerThrow );
   System.out.print (gameObject.bigWinner(winner, rounds));
}
    //Final Output
     System.out.print (gameObject.bigWinner(winner, rounds));
  }
//accessor to return round to RPSGame
   public  int getRound (int round){
     this.round = round;
     return round;
  }
}

游戏方法:

  import java.util.*;

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

     Random generator = new Random ();
     Scanner sc = new Scanner(System.in);
     int rounds = mainRPS.getRound(rounds);
   }
   //Random Throw Generator
   public  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  int getPlayerThrow (){
     RPSPlayer player = new RPSPlayer();
     int getPThrow = player.makeThrow();
     return getPThrow;
  }

 //Does all of the calculatoins and ouputs who threw what.
   public  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;
  }


 }

玩家方法:

   import java.util.*;

  public class RPSPlayer {

  public  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  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  int getThrow (int playerThrow){
    this.playerThrow = playerThrow;
     return playerThrow;
  }


 }

当我尝试编译任何类时,错误引用RPSPlayer类中的代码:

cannot find symbol - variable playerThrow

正如我之前所说,我对面向对象程序的了解非常薄弱。我不确定为什么我会收到这个错误,因为int playerThrow被定义在它之上。我也不确定我的代码中是否还有其他缺陷或错误。我特别挣扎的一件事是静态代码与非静态代码的概念,以及何时使用它们以及可以在其中使用的内容。

非常感谢您的建议。先感谢您。

1 个答案:

答案 0 :(得分:1)

在为其分配值之前,您需要定义成员变量。对于每个类和每个成员变量,这是强制性的(显然)。 请将此添加到您的班级:

private int playerThrow;

此外,函数getThrow毫无意义。 Getters返回已设置的值; setter是实际为成员变量赋值的人。这样做:

public int getThrow()
{
    return playerThrow;
}

public void setThrow(int playerThrow)
{
    this.playerThrow = playerThrow;
}

请注意,this.playerThrowplayerThrow无论如何都不相关。 this.playerThrow是类的成员变量(如上所示定义),playerThrow是作为方法参数的变量。

修改

我正在解决你的一些错误。

  1. 您在评论中提到的问题是因为您在调用类mainRPS时调用构造函数RPSmain。它位于RPSMain mainRPS = new mainRPS();类的RPSGame位于此处。请将此问题修改为RPSMain mainRPS = new RPSMain();
  2. 请在您的RPSMain课程中定义private int round。这与你的第一个错误相同。
  3. 另外,在int winner之外定义for loop,因为您也在循环之外使用它。当你在循环中定义一个值时,它将是该循环的本地值,并在程序退出循环时被销毁。