我正在研究一些java,试图制作Big Bang系列Rock Paper系列

问题描述 投票:0回答:2

我是Java和编程的新手,我正在尝试制作摇滚纸剪刀Big Bang的电视节目风格。我目前只是坚持一些事情,我希望得到一些帮助。我目前遇到了结束分层while循环的问题,所以当胜利者赢了2场比赛时它结束了。此外,我的用户输入有问题。任何帮助,将不胜感激。这是我目前的代码。

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

public class rockPaperScissorsSpock {
public static void main(String[] args) {

    String userMove; // User will input moved of P, R, S, O and L as moves
    String computerMove = ""; // Computer input move
    int computerInput; // number generator that will determine the computers move
    int userScore = 0;
    int computerScore=0;
    int numberOfGames=0;


    /* this is the introduction to the game. And explaining the rules */

    System.out.println(
            "Welcome to the game of Paper Rock Scissors Spock!\n\nTHE RULES OF THIS GAME ARE AS FOLLOWS: Scissors cuts paper. Paper covers Rock. Rock crushes lizard."
                    + "Lizard poisons spock. Spock smashes scissors.\nSpock vaporizes rock. Rock crushes scissors. Scissors decapitates lizard."
                    + "Lizard eats paper. And paper disproves spock.\n ");
    System.out.println(
            "To play the moves, you must input the variable associated:\n Paper = P\n Rock = R\n Scissors = S\n Spock = O\n Lizard =L\n\n");

while(true){
        do {
    // getting the user input
    Scanner scan = new Scanner(System.in);
    Random randomGenerator = new Random();

    System.out.println();

    // this creates a random number generator for the computers. The number will be
    // associated with a move
    computerInput = randomGenerator.nextInt(5) + 1;

    // assigning moves to an Int
    if (computerInput == 1) {   
        computerMove = "P";
    }
    else if (computerInput == 2) {
        computerMove = "R";}
    else if (computerInput == 3) {
        computerMove = "S";}
    else if (computerInput == 4) {
        computerMove = "O";}
    else if (computerInput == 5) {
        computerMove = "L";
    }


    // prompts the user to enter their move
    System.out.println("Please enter your move:");
    userMove = scan.next();

    // Gathers and displays the users move
    userMove = userMove.toUpperCase();
    // Reporting to the player which contestants move was played
    System.out.printf("Your move was:" + userMove + "\n");
    System.out.printf("The Computer played against you: " + computerMove + "\n");



    if (computerMove.equals(userMove)) {
        System.out.printf("You both played the same move. Neither of you are winners....\n");
        numberOfGames++;
        System.out.printf("You have played:" +numberOfGames +"game(s)\n\n");
    }
    else if (computerMove.equals("P")){
     if (userMove.equals("S")) 
            userScore++;
            numberOfGames++;
            System.out.printf("Scissors cuts paper. Congrats You Win!\n");
            System.out.printf("Your Score is:" + userScore +"\n");
            System.out.printf("You have played:" +numberOfGames +"game(s)\n\n");

        } 
    else if (computerMove.equals("S")){
    if (userMove.equals("P")) 
                System.out.printf("Scissors cuts paper. Sad day. you lost.\n");
                computerScore++;
                numberOfGames++;
                System.out.printf("Computer Score is:" + computerScore+"\n");
                System.out.printf("You have played:" +numberOfGames +"game(s)\n\n");

                }
    else if (computerMove.equals("P")){
    if (userMove.equals("O")) 
                System.out.printf("Spock is disproven by paper. Sad day. You lost!\n\n");
                computerScore++;
                numberOfGames++;
                System.out.printf("Computer Score is:"+computerScore+"\n");
                System.out.printf("You have played:" +numberOfGames +"game(s)\n\n");
                }

    else if (computerMove.equals("O")){
         if (userMove.equals("P")) 

                System.out.printf("Spock is disproven by paper. Congrats You Win!\n");
                userScore++;
                numberOfGames++;    
                System.out.printf("Your Score is:" +userScore+"\n");
                System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");        
                } 
    else if (computerMove.equals("P")) {
        if (userMove.equals("R"))
                System.out.printf("Paper covers rock. Sad day. You lost!\n");
                computerScore++;
                numberOfGames++;
                System.out.printf("Computer Score is:"+ computerScore+"\n");
                System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");

                            }
        else if (computerMove.equals("P")){
        if (userMove.equals("L")) 
            System.out.printf("Lizard eats paper. Congrats you win!\n");
            userScore++;
            numberOfGames++;
            System.out.printf("Computer Score is:"+ userScore+"\n");
            System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");

                    }

    else if (computerMove.equals("L")){
        if (userMove.equals("P")) 
                System.out.printf("Lizard eats paper. Sad day. You lost!\n");
                computerScore++;
                numberOfGames++;
                System.out.printf("Computer Score is:"+ computerScore+"\n");
                System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");
                } 


        else if (computerMove.equals("R")){
         if (userMove.equals("P")) 
                userScore++;
                numberOfGames++;
                System.out.printf("Paper covers rock. Congrats You Win!\n");
                System.out.printf("Your Score is:"+ userScore+"\n");
                System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");

                            } 
        else if (computerMove.equals("R")){
            if (userMove.equals("O")) 
                System.out.printf("Spock crushes Rock. Congrats you win!\n");
                userScore++;
                numberOfGames++;
                System.out.printf("Computer Score is:" +userScore+"\n");
                System.out.printf("You have played:" +numberOfGames +"game(s)\n\n");
                }

            else if (computerMove.equals("L")){
                if (userMove.equals("O")) 
                    System.out.printf("Lizard poisons Spock. Sad day. You lost!\n");
                    computerScore++;
                    numberOfGames++;
                    System.out.printf("Computer Score is:" + userScore+"\n");
                    System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");
                } 
            else if (computerMove.equals("O")){
                if (userMove.equals("R")) 
                System.out.printf("Spock crushes Rock. Sad day. You lost!\n");
                computerScore++;
                numberOfGames++;
                System.out.printf("Computer Score is:" + computerScore+"\n");
                System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");
                } 
            else if (computerMove.equals("O")){
                if (userMove.equals("L")) 
                    System.out.printf("Lizard poisons Spock. Congrats you win!\n");
                    userScore++;
                    numberOfGames++;
                    System.out.printf("Computer Score is:" + userScore+"\n");
                    System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");
                    }
                else if (computerMove.equals("O")) {
                    if (userMove.equals("S"))
                        System.out.printf("Spock smashes Scissors. Sad day. You lost!\n");
                        computerScore++;
                        numberOfGames++;
                        System.out.printf("Computer Score is:"+ computerScore+"\n");
                        System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");
                    } 
                else if (computerMove.equals("S")){
                    if (userMove.equals("O")) 
                        System.out.printf("Spock smashes Scissors. Congrats you win!\n");
                        userScore++;
                        numberOfGames++;
                        System.out.printf("Computer Score is:" + userScore+"\n");
                        System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");
                                            }               
                    else if (computerMove.equals("S")){
                        if (userMove.equals("L")) 
                            System.out.printf("Scissors decapitatees Lizard. Sad day. You lost!\n");
                            computerScore++;
                            numberOfGames++;
                            System.out.printf("Computer Score is:" +computerScore+"\n");
                            System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");
                        } 
                    else if (computerMove.equals("L")){
                        if (userMove.equals("S")) 
                            System.out.printf("Scissors decapitates Lizard. Congrats you win!\n");
                            userScore++;
                            numberOfGames++;
                            System.out.printf("Computer Score is:" + userScore+"\n");
                            System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");

                            }
                        else if (computerMove.equals("R")){
                            if (userMove.equals("S")) 
                                System.out.printf("Rock crushes Scissors. Sad day. You lost!\n");
                                computerScore++;
                                numberOfGames++;
                                System.out.printf("Computer Score is:"+ computerScore+"\n");
                                System.out.printf("You have played:"+ numberOfGames +"game(s)\n\n");
                            } 
                        else if (computerMove.equals("S")){
                            if (userMove.equals("R")) 
                                System.out.printf("Rock crushes Scissors. Congrats you win!\n");
                                userScore++;
                                numberOfGames++;
                                System.out.printf("User Score is:" + userScore+"\n");
                                System.out.printf("You have played:"+numberOfGames +"game(s)\n\n");
                            }
                        else if(!(userMove.equals("O") && userMove.equals("R") && userMove.equals("O") && userMove.equals("S") && userMove.equals("P") )) {
                            System.out.println("Invalid Entry. Please try again.");
                        }

    }while(2==(computerScore -userScore));

    }



}
}
java
2个回答
1
投票

如果你拿出你的while(true)循环,它应该在以太玩家赢2时停止游戏


0
投票

如果没有其他许多,你可以使这一点变得更容易。如果创建一个像5-Sc,4-P,3-R,2-L,1-S的地图(hashmap),你可以看到一个获胜的模式。如果差异是均匀的,那么第一次获胜。如果差异是奇数,那么第二个胜利。例如P - R = 1 - 纸覆盖岩石,Sc - L = 3 - 剪刀斩首蜥蜴,R - S = 2 - 在这种情况下Spock vaporaze rock和第二次获胜。 (如果答案是否定的话,它都会反转)并且摆脱双循环,1就足够了

© www.soinside.com 2019 - 2024. All rights reserved.