1

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

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

  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 中搜索变量?

4

4 回答 4

3

对于这些错误,在您的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不是 in 中的变量RPSMain,它只是 in 中的变量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));

如果要保留intannounceWinner()方法返回的值,则必须通过进行以下调整来捕获它...

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()将存储在类中调用的局部变量winnerRPGMain。现在当它尝试在下一行winner的方法中使用该变量时gameObject.bigWinner(),它知道该值。

要修复您的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在您的程序中。

于 2012-05-19T04:55:45.820 回答
1

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

其他更可取的解决方案是让它们在 class 中受保护的实例变量RPSPlayer。这样,它们将可供两者使用,也可RPSGame通过RPSMain继承使用。

于 2012-05-19T05:00:47.863 回答
0

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

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

RSPGame.makeCompThrow();

而不是这个

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

这应该生成编译器警告。

于 2012-05-19T05:00:13.317 回答
0

I'll nitpick your makeThrow method:

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

that will never loop, because no number is smaller than 1 AND (&&) bigger than 3. It should read

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

Bigger than 3 OR (||) smaller than 1. Now it should work as intended.

于 2012-05-21T14:36:57.987 回答