*更新*现在我的程序编译但不运行?

时间:2015-04-26 17:41:16

标签: java methods

我正在编写一个程序,提示用户在计算机上玩石头剪刀。我无法从main方法调用int rpsls方法。我不知道如何从main方法调用它,因为我需要将main方法中的用户输入值输入到rpsls方法中。以下是我到目前为止所做的,是的,它充满了错误,但我主要关心的是调用方法。

UPDATE - 我添加了更多代码,现在程序将编译。但是,当我尝试运行它时它只是冻结。任何人都可以帮助找到使程序运行的解决方案吗?

import java.util.Scanner; 
public class Rpsls
{  



   public static void main (String [] args) {
      int wins = 0;
      int ties = 0;
      int losses = 0;
      int retVal = 0;
      int output = rpsls(retVal);
      //while loop 
      Scanner input = new Scanner (System.in);
      int x = input.nextInt();
         while (x > 0 && x <= 5) {
            //input gesture 

            System.out.println(" ROCK PAPER SCISSORS LIZARD SPOCK");
            System.out.println(" (1)   (2)     (3)     (4)    (5)");
            System.out.print("Enter your choice, or 0 to end: ");

            //call rpsls for inputted gesture
            int gesture = input.nextInt();
            rpsls(gesture);
            //returned values: loss, win, or tie
            if (output == -1 ) {
               losses++;
            }
               else if (output == 0) {
                  ties++;
               }
            else {
               wins++;
            }
            //count wins and losses 
            //end while loop 
            //print sum of wins and losses. 
         }
         System.out.println("You won " + wins + " games, lost " + losses + " games, and tied " + ties + " games.");

   }

   public static int rpsls(int gesture) {
      //generate random gesture for computer 
      int attack = (int)(Math.random()*5);
      //decide who won 
           int wins = 0; int losses = 0; int ties = 0;
           int retVal = 0;     

            if (gesture == 1 && attack == 1) {
               System.out.println("You chose ROCK.");
               System.out.println("Computer chose ROCK.");
               System.out.println("It's a tie!");
               retVal = 0;
            }
            if (gesture == 1 && attack == 2) {
               System.out.println("You chose ROCK.");
               System.out.println("Computer chose PAPER.");
               System.out.println("PAPER covers ROCK.");
               System.out.println("Computer wins!");
               retVal = -1;
            }
            if (gesture == 1 && attack == 3) {
               System.out.println("You chose ROCK.");
               System.out.println("Computer chose SCISSORS.");
               System.out.println("ROCK crushes SCISSORS.");
               System.out.println("You win!");
               retVal = 1;
            }
            if (gesture == 1 && attack == 4) {
               System.out.println("You chose ROCK.");
               System.out.println("Computer chose LIZARD.");
               System.out.println("ROCK crushes LIZARD.");
               System.out.println("You win!");
               retVal = 1;
            }
            if (gesture == 1 && attack == 5) {
               System.out.println("You chose ROCK.");
               System.out.println("Computer chose SPOCK.");
               System.out.println("SPOCK vaporizes ROCK.");
               System.out.println("Computer wins!");
               retVal = -1;
            }
            if (gesture == 2 && attack == 1) {
               System.out.println("You chose PAPER.");
               System.out.println("Computer chose ROCK.");
               System.out.println("PAPER covers ROCK.");
               System.out.println("You win!");
               retVal = 1;
            }
            if (gesture == 2 && attack == 2) {
               System.out.println("You chose PAPER.");
               System.out.println("Computer chose PAPER.");
               System.out.println("It's a tie!");
               retVal = 0;
            }
            if (gesture == 2 && attack == 3) {
               System.out.println("You chose PAPER.");
               System.out.println("Computer chose SCISSORS.");
               System.out.println("SCISSORS cut PAPER.");
               System.out.println("Computer wins!");
               retVal = -1;
            }
            if (gesture == 2 && attack == 4) {
               System.out.println("You chose PAPER.");
               System.out.println("Computer chose LIZARD.");
               System.out.println("LIZARD eats PAPER.");
               System.out.println("Computer wins!");
               retVal = -1;
            }
            if (gesture == 2 && attack == 5) {
               System.out.println("You chose PAPER.");
               System.out.println("Computer chose SPOCK.");
               System.out.println("PAPER disproves SPOCK.");
               System.out.println("You win!");
               retVal = 1;
            }
            if (gesture == 3 && attack == 1) {
               System.out.println("You chose SCISSORS.");
               System.out.println("Computer chose ROCK.");
               System.out.println("ROCK crushes SCISSORS.");
               System.out.println("Computer wins!");
               retVal = -1;
            }
            if (gesture == 3 && attack == 2) {
              System.out.println("You chose SCISSORS.");
              System.out.println("Computer chose PAPER.");
              System.out.println("SCISSORS cut PAPER.");
              System.out.println("You win!");
              retVal = 1;
            }
            if (gesture == 3 && attack == 3) {
              System.out.println("You chose SCISSORS.");
              System.out.println("Computer chose SCISSORS.");
              System.out.println("It's a tie!");
              retVal = 0;
            }
            if (gesture == 3 && attack == 4) {
              System.out.println("You chose SCISSORS.");
              System.out.println("Computer chose LIZARD.");
              System.out.println("SCISSORS decapitate LIZARD.");
              System.out.println("You win!");
              retVal = 1;
            }
            if (gesture == 3 && attack == 5) {
              System.out.println("You chose SCISSORS.");
              System.out.println("Computer chose SPOCK.");
              System.out.println("SPOCK smashes SCISSORS.");
              System.out.println("Computer wins!");
              retVal = -1;
            }
            if (gesture == 4 && attack == 1) {
              System.out.println("You chose LIZARD.");
              System.out.println("Computer chose ROCK.");
              System.out.println("ROCK crushes LIZARD.");
              System.out.println("Computer wins!");
              retVal = -1;
            }
            if (gesture == 4 && attack == 2) {
              System.out.println("You chose LIZARD.");
              System.out.println("Computer chose PAPER.");
              System.out.println("LIZARD eats PAPER.");
              System.out.println("You win!");
              retVal = 1;
            }
            if (gesture == 4 && attack == 3) {
              System.out.println("You chose LIZARD.");
              System.out.println("Computer chose SCISSORS.");
              System.out.println("SCISSORS decapitate LIZARD.");
              System.out.println("Computer wins!");
              retVal = -1;
            }
            if (gesture == 4 && attack == 4) {
              System.out.println("You chose LIZARD.");
              System.out.println("Computer chose LIZARD.");
              System.out.println("It's a tie!");
              retVal = 0;
            }
            if (gesture == 4 && attack == 5) {
              System.out.println("You chose LIZARD.");
              System.out.println("Computer chose SPOCK.");
              System.out.println("LIZARD poisons SPOCK.");
              System.out.println("You win!");
              retVal = 1;
            }
            if (gesture == 5 && attack == 1) {
              System.out.println("You chose SPOCK.");
              System.out.println("Computer chose ROCK.");
              System.out.println("SPOCK vaporizes ROCK.");
              System.out.println("You win!");
              retVal = 1;
            } 
            if (gesture == 5 && attack == 2) {
              System.out.println("You chose SPOCK.");
              System.out.println("Computer chose PAPER.");
              System.out.println("PAPER disproves SPOCK.");
              System.out.println("Computer wins!");
              retVal = -1;
            }
            if (gesture == 5 && attack == 3) {
              System.out.println("You chose SPOCK.");
              System.out.println("Computer chose SCISSORS.");
              System.out.println("SPOCK smashes SCISSORS.");
              System.out.println("You win!");
              retVal = 1;
            }
            if (gesture == 5 && attack == 4) {
              System.out.println("You chose SPOCK.");
              System.out.println("Computer chose LIZARD.");
              System.out.println("LIZARD poisons SPOCK.");
              System.out.println("Computer wins!");
              retVal = -1;
            }
            if (gesture == 5 && attack == 5) {
              System.out.println("You chose SPOCK.");
              System.out.println("Computer chose SPOCK.");
              System.out.println("It's a tie!");
              retVal = 0;
            } 
            return retVal;
   }

}

3 个答案:

答案 0 :(得分:2)

这一行:

int rpsls = gesture(x);

...在int函数中创建名为rpsls的{​​{1}}变量,调用名为main的方法,并将gesture传递给它。< / p>

你可能意味着:

x

...在int gesture = input.nextInt(); rpsls(gesture); 上调用nextInt以获取用户的号码,将其记为Scanner,然后调用您现有的gesture 1}}方法,传入rpsls

答案 1 :(得分:0)

  1. 您正在调用 gesture(int)并且您可能正在尝试调用 int rpsls(int gesture) int retVal = rpsls(gesture);
  2. 某些变量在代码块内被初始化并声明,这会影响其生命周期,例如input位于while块内,因此它不会在那之外工作;
  3. int rpsls(int gesture)设计不合理:计算机应为岩石/纸/剪刀随机取值,然后将其与用户进行比较;

答案 2 :(得分:0)

您的代码不起作用,至少需要:

  • int x = input.nextInt();语句后移动while (x > 0 && x <= 5),如果没有这个,它将不会使用您输入的游戏,
  • 在第一次使用它之前移动扫描仪声明,否则它将无法编译,
  • 在'手势'上改变rpsls方法中的所有'x',rpsls方法无法访问'x'变量形式main方法;
  • 初始化'retVal'变量(给它一个值),为exaple 0,以避免编译错误;
  • 通过此更改int rpsls = gesture(x);,例如int rpsls = rpsls(x);,它将继续输入

经过这些修改后,我觉得它运作正常。

编辑: 你应该从main删除int output = rpsls(retVal);和retVal声明,我认为这是无用的,并在int output = rpsls(x);之后添加x = input.nextInt();然后它正常工作。 同时将int x = input.nextInt();替换为int gesture = input.nextInt();