简体   繁体   English

java初学者石头剪刀布游戏

[英]java beginner rock paper scissors game

I am working on a "Rock, Paper, Scissors" game for my intro java class.我正在为我的介绍 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.让用户输入 1、2 或 3 的数字,每个代表三个选项之一。 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..到目前为止编辑的代码,它说我的letsPlay方法缺少返回语句:不确定如何继续..

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.您没有将 String 答案传递给您的letsPlay() 方法,这是因为您的letsPlay() 方法不能将String 作为参数,因为它是在没有传递参数的情况下定义的。 A solution to this problem is to change the method definition to require a String variable.解决此问题的方法是将方法定义更改为需要 String 变量。

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.然后在您的方法中,您在 letPLay(String userInput) 方法中使用变量 userInput 而不是 String answer。

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.一旦游戏结束,您应该退出游戏,并在您的 main() 方法中询问用户是否想再次玩游戏。

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.首先,在您的主要方法中,一个input.next()是多余的并且没有用,因此请将其删除。 Now write a statement in main method as follows after String answer = input.next();现在在 main 方法中在String answer = input.next();之后编写如下语句:

letsPlay(answer);

Put a parameter in letsPlay() method as follows:在letsPlay()方法中放一个参数如下:

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你可以打电话给main

Move codes to main from letsPlay method.将代码从letsPlay 方法移到main。

remove: letsPlay() use: main(null)删除: letsPlay()使用: 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.发生 cMove 未初始化异常。 So use this:所以使用这个:

int cMove = 0;

Now, any errors not occurred.现在,没有发生任何错误。

Move the Scanner object inside the constructor.在构造函数中移动扫描仪 object。 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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