简体   繁体   中英

java beginner rock paper scissors game

I am working on a "Rock, Paper, Scissors" game for my intro java class. Here is the prompt: Create a game of "Rock, Paper, Scissors" where the computer randomly chooses rock, paper, or scissors. Let the user enter a number of 1, 2, or 3, each representing one of three choices. Determine a winner. Game should ask the user to play again and continue if yes and stop if no. Once the user stops playing the program should print the total number of wins.

I am having issues with declaring my variables in the correct places since I am trying to use a method so I can call it to play the game again.

import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors 
{
public static void main (String[] args) 
{    
    Scanner input = new Scanner(System.in);
    System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
    System.out.println("Answer \"yes\" or \"no\"");
    input.next();
    String answer = input.next();
}

    
public static int letsPlay()
{
    int cMove; 
    int userMove = 0; 
    int cScore = 0; 
    int pScore = 0; 
    int tie = 0;
    int rounds = 0; 
    Random r = new Random();
    

    while (answer.equalsIgnoreCase("yes")) 
        cMove = r.nextInt(3)+1;
        System.out.println("Choose your move!");
        System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
        userMove = input.nextInt(); 
        while(input.hasNextInt()) {
            if (userMove!=1 && userMove!=2 && userMove!=3)
            {
                System.out.println("Invalid move. Try again.");
                System.out.println("Enter your move: ");
                input.nextInt();
            }
        } 
        if(userMove==1)
        {
            System.out.println("You have chosen Rock!");  
        }
        else if(userMove==2)
        {
            System.out.println("You have chosen Paper!");  
        }
        else if(userMove==3)
        {
            System.out.println("You have chosen Scissors!");  
        }

            if (userMove == cMove) 
            { 
                System.out.println("Tie Game!");
                System.out.println("");
                tie++;
                rounds++;
            } else if (cMove==1 && userMove==3)
                {
                    System.out.println("Computer chose Rock!");
                    System.out.println("Rock beats Scissors!");
                    System.out.println("Computer Wins!");
                    cScore++;
                    rounds++;
                } 

                else if (cMove==1 && userMove==2) 
                {
                    System.out.println("Computer chose Rock!");
                    System.out.println("Paper beats Rock!");
                    System.out.println("Player Wins!");
                    pScore++;
                    rounds++;
                } 

                else if (cMove==2 && userMove==3) 
                {
                    System.out.println("Computer chose Paper!");
                    System.out.println("Scissors beats Paper!");
                    System.out.println("Player Wins!");
                    pScore++;
                    rounds++;
                } 

                else if (cMove==2 && userMove==1)
                {
                    System.out.println("Computer chose Paper!");
                    System.out.println("Paper beats Rock!");
                    System.out.println("Computer Wins!");
                    cScore++;
                    rounds++;
                } 

                else if (cMove==3 && userMove==1)  
                {
                    System.out.println("Computer chose Scissors!");
                    System.out.println("Rock beats Scissors!");
                    System.out.println("Player Wins!");
                    pScore++;
                    rounds++;
                } 

                else if (cMove==3 && userMove==2) 
                {
                    System.out.println("Computer chose Scissors!");
                    System.out.println("Scissors beats Paper!");
                    System.out.println("Computer Wins!");
                    cScore++;
                    rounds++;
                }
            
        System.out.println("Would you like to play again?");
        System.out.println("Answer \"yes\" or \"no\"");
        input.next();
        String yesorno = input.next();
        if(yesorno.equalsIgnoreCase("yes"))
        {
            letsPlay();
        }
        else {
            System.out.println ("Here are the final scores after " + rounds +" rounds:");
            System.out.println ("You: "+ pScore + "Computer: "+ cScore + "Ties: " + tie);
            }
    }    
}

Edited code so far, it says missing return statement from my letsPlay method: Not sure how to proceed..

import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors 
{
public static void main (String[] args) 
{    
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
System.out.println("Answer \"yes\" or \"no\"");
String answer = input.next();
letsPlay(answer);
}



public static int letsPlay(String answer)
{
int cMove; 
int userMove = 0; 
int cScore = 0; 
int pScore = 0; 
int tie = 0;
int rounds = 0; 
Random r = new Random();
Scanner input = new Scanner(System.in);
cMove = r.nextInt(3)+1;

while (answer.equalsIgnoreCase("yes")) 
    
    System.out.println("Choose your move!");
    System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
    userMove = input.nextInt(); 
    while(input.hasNextInt()) {
        if (userMove!=1 && userMove!=2 && userMove!=3)
        {
            System.out.println("Invalid move. Try again.");
            System.out.println("Enter your move: ");
            input.nextInt();
            
        }
    } 
    if(userMove==1)
    {
        System.out.println("You have chosen Rock!");  
    }
    else if(userMove==2)
    {
        System.out.println("You have chosen Paper!");  
    }
    else if(userMove==3)
    {
        System.out.println("You have chosen Scissors!");  
    }

        if (userMove == cMove) 
        { 
            System.out.println("Tie Game!");
            System.out.println("");
            tie++;
            rounds++;
        } else if (cMove==1 && userMove==3)
            {
                System.out.println("Computer chose Rock!");
                System.out.println("Rock beats Scissors!");
                System.out.println("Computer Wins!");
                cScore++;
                rounds++;
            } 

            else if (cMove==1 && userMove==2) 
            {
                System.out.println("Computer chose Rock!");
                System.out.println("Paper beats Rock!");
                System.out.println("Player Wins!");
                pScore++;
                rounds++;
            } 

            else if (cMove==2 && userMove==3) 
            {
                System.out.println("Computer chose Paper!");
                System.out.println("Scissors beats Paper!");
                System.out.println("Player Wins!");
                pScore++;
                rounds++;
            } 

            else if (cMove==2 && userMove==1)
            {
                System.out.println("Computer chose Paper!");
                System.out.println("Paper beats Rock!");
                System.out.println("Computer Wins!");
                cScore++;
                rounds++;
            } 

            else if (cMove==3 && userMove==1)  
            {
                System.out.println("Computer chose Scissors!");
                System.out.println("Rock beats Scissors!");
                System.out.println("Player Wins!");
                pScore++;
                rounds++;
            } 

            else if (cMove==3 && userMove==2) 
            {
                System.out.println("Computer chose Scissors!");
                System.out.println("Scissors beats Paper!");
                System.out.println("Computer Wins!");
                cScore++;
                rounds++;
            }
        
        
    System.out.println("Would you like to play again?");
    System.out.println("Answer \"yes\" or \"no\"");
    input.next();
    answer = input.next();
    
    if(answer.equalsIgnoreCase("yes"))
    {
        main(null);
    }
    else {
        System.out.println ("Here are the final scores after " + rounds +" 
rounds:");
        System.out.println ("You: "+ pScore + "Computer: "+ cScore + "Ties: " 
+ tie);
        }
    }
}

You aren't passing the String answer to your letsPlay() method and that's because your letsPlay() method can't take a String as a parameter because it is defined without parameters being passed. A solution to this problem is to change the method definition to require a String variable.

public static int letsPlay()

turns into

public static int letsPlay(String userInput)

then inside your method you use the variable userInput instead of String answer in the letsPLay(String userInput) method.

The next issue you run into is you're calling the method again within the method. This is called recursion and it's perfectly legal, however it is not ideal in this circumstance. You should exit the game once it's over and ask the user in your main() method if they'd like to play again.

public static void main (String[] args) 
{    
    Scanner input = new Scanner(System.in);
    
    do
    {
        System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
        System.out.println("Answer \"yes\" or \"no\"");
        String answer = input.nextLine();
        letsPlay(answer);

    }while(answer.equalsIgnoreCase("yes"));

}

Firstly, in your main method one input.next() is extra and of no use so remove it. Now write a statement in main method as follows after String answer = input.next();:

letsPlay(answer);

Put a parameter in letsPlay() method as follows:

public static void letsPlay(String answer) {
//Your code..........
//Some last edits...
Scanner input = new Scanner(System.in);
answer = input.next();
if(!(answer.equalsIgnoreCase("yes")))
{
    System.out.println ("Here are the final scores after "+rounds+"        
    rounds:");
    System.out.println("You:"+pScore+"Computer: "+cScore+"Ties: "+tie);
}
}

No required extra method for calling any line. You can call main

Move codes to main from letsPlay method.

remove: letsPlay() use: main(null)

import java.util.Scanner;
import java.util.Random;


public class Main 
{
    public static void main (String[] args) 
    {    
        Scanner input = new Scanner(System.in);
        System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
        System.out.println("Answer \"yes\" or \"no\"");
        input.next();
        String answer = input.next();
        
        // moved codes to following place from letsPlay
        int cMove = 0; 
        ...
        if(yesorno.equalsIgnoreCase("yes"))
        {
            main(null); // changed with letsPlay()
        }
       ...
    }
}

cMove not initilazed exception occurred. So use this:

int cMove = 0;

Now, any errors not occurred.

Move the Scanner object inside the constructor. Currently you have it outside and your code does not know what it is.

Here is the modified portion of your code:

public static int letsPlay()
{
    int cMove; 
    int userMove = 0; 
    int cScore = 0; 
    int pScore = 0; 
    int tie = 0;
    int rounds = 0; 
    Random r = new Random();
    
    Scanner input = new Scanner(System.in); // move the input object inside the constructor

Hope it helps.

There are many errors in your code. You can check out the comments in the code for a proper understanding of the code.

import java.util.Scanner;
import java.util.Random;

public class RockPaperScissors {
    public static void main(String[] args) {
        // initialising variable to 0 for score calculation
        int cScore = 0;
        int pScore = 0;
        int tie = 0;
        int rounds = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("Would you like to play \"Rock, Paper, Scissors?\"");
        System.out.println("Answer \"Yes\" or \"No\"");
        String answer = input.next();
        if (answer.equalsIgnoreCase("yes")) {
            // Calling method letsPlay with arguments answer, cScore, pScore, tie, rounds
            // initially cScore = pScore = tie = rounds = 0
            letsPlay(answer, cScore, pScore, tie, rounds);
        }
    }

    // letsPlay Method
    public static void letsPlay(String answer, int cScore, int pScore, int tie, int rounds) {
        int cMove;
        int userMove;
        Random r = new Random();
        Scanner input = new Scanner(System.in);

        // loop untill user chose no
        while (true) {
            // to get random move of computer on every iteration
            cMove = r.nextInt(3) + 1;
            System.out.println("--------------------------------------------------");
            System.out.println("Choose your move!");
            System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
            userMove = input.nextInt();

            // loop until user input number 1 or 2 or 3
            while (userMove != 1 && userMove != 2 && userMove != 3) {
                System.out.println("Invalid move. Try again.");
                System.out.println("--------------------------------------------------");
                System.out.println("Choose your move: ");
                System.out.println("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ");
                userMove = input.nextInt();
            }

            // Print statement for user move
            if (userMove == 1) {
                System.out.println("You have chosen Rock!");
            } else if (userMove == 2) {
                System.out.println("You have chosen Paper!");
            } else {
                System.out.println("You have chosen Scissors!");
            }

            // Print statement for computer move
            if (cMove == 1) {
                System.out.println("Computer chose Rock!");
            } else if (cMove == 2) {
                System.out.println("Computer chose Paper!");
            } else {
                System.out.println("Computer chose Scissors!");
            }

            // Winning, Loosing and Tie conditions
            // increment round to 1 every time
            // increment the winner, looser or tie on every iteration
            if (userMove == cMove) {
                System.out.println("Tie Game!");
                tie++;
                rounds++;
            } else if (cMove == 1 && userMove == 3) {
                System.out.println("Rock beats Scissors!");
                System.out.println("Computer Wins!");
                cScore++;
                rounds++;
            } else if (cMove == 1 && userMove == 2) {
                System.out.println("Paper beats Rock!");
                System.out.println("Player Wins!");
                pScore++;
                rounds++;
            } else if (cMove == 2 && userMove == 3) {
                System.out.println("Scissors beats Paper!");
                System.out.println("Player Wins!");
                pScore++;
                rounds++;
            } else if (cMove == 2 && userMove == 1) {
                System.out.println("Paper beats Rock!");
                System.out.println("Computer Wins!");
                cScore++;
                rounds++;
            } else if (cMove == 3 && userMove == 1) {
                System.out.println("Rock beats Scissors!");
                System.out.println("Player Wins!");
                pScore++;
                rounds++;
            } else if (cMove == 3 && userMove == 2) {
                System.out.println("Scissors beats Paper!");
                System.out.println("Computer Wins!");
                cScore++;
                rounds++;
            }

            // Asking again to play or not
            System.out.println("\nWould you like to play again?");
            System.out.println("Answer \"Yes\" or \"No\"");
            answer = input.next();

            if (answer.equalsIgnoreCase("yes")) {
                // If yes the call letsPlay(answer, cScore, pScore, tie, rounds);
                // But this time value of cScore, pScore, tie, rounds is changed
                // according to conditions
                letsPlay(answer, cScore, pScore, tie, rounds);
            } else {
                // Print if user says didn't want to play again
                System.out.println("==========================================");
                System.out.println("\nHere are the final scores after " + rounds + " rounds:");
                System.out.println("You      : " + pScore + "\nComputer : " + cScore + "\nTies     : " + tie);
            }
            // Exit if user didn't want to play again
            break;
        }
    }
}

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