简体   繁体   中英

*UPDATE* now my program compiles but doesn't run?

I am writing a program that prompts the user to play rock paper scissors against the computer. I'm having trouble calling the int rpsls method from the main method. I am not sure how to call it from the main method because I need the value of user input from the main method to be inputted into the rpsls method. The following is what I have so far, and yes it's full of errors, but I am mainly concerned with calling the method.

UPDATE - I added some more code, and now the program will compile. However, when I try to run it it just freezes. Can anyone help find a solution to make the program run?

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

}

This line:

int rpsls = gesture(x);

...creates an int varaible called rpsls in your main function, calls a method called gesture , and passes x into it.

You may have meant:

int gesture = input.nextInt();
rpsls(gesture);

...which calls nextInt on your Scanner to get a number from the user, remembers it as gesture , then calls your existing rpsls method, passing in gesture .

  1. You are calling gesture(int) and you were probably trying to invoke int rpsls(int gesture) int retVal = rpsls(gesture) ;
  2. Some variables are initialized and declared inside code blocks that affects its lifetime, for example input is inside the while block so it will not work outside that;
  3. int rpsls(int gesture) is poorly designed: the computer should shoose a random value for rock/paper/scissors and then compare it with the user;

Your code doesn't work, you need to at least:

  • move int x = input.nextInt(); after while (x > 0 && x <= 5) statement, without this it will not use you input for game,
  • move Scanner declaration before first use of it, couse it will not compile,
  • change all 'x' in rpsls method on 'gesture', the rpsls method don't have an access to 'x' variable form main method;
  • initialize 'retVal' variable (give it a value), for exaple 0, to avoid compilation error;
  • change int rpsls = gesture(x); for for example int rpsls = rpsls(x); by this, it will proceed on your input

After those changes, it works fine in my opinion.

EDIT: You should remove int output = rpsls(retVal); and retVal declaration from main, couse it is useless i think, and add int output = rpsls(x); after x = input.nextInt(); then it works properly. Also replace int x = input.nextInt(); with int gesture = input.nextInt();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM