从岩石剪刀代码获取结果的麻烦(java)

时间:2015-04-24 19:05:31

标签: java methods

因此,当我运行我的代码时,它一直说这是一个平局因为我认为我的一个方法存在问题。

以下是我的全部代码:

   String userChoice = "";
        String computerChoice = "";
        System.out.println("Welcome to Rock, Paper, Scissors.");
        System.out.println("The rules of the game are Rock breaks Scissors, "
                + "Scissors cut Paper, and Paper covers Rock. In this game, "
                + "the user will play against the computer.");
        System.out.println("The legend for this game is: R = rock, P = paper,"
                + " and S = scissors.");

        generateUserChoice();
        generateComputerChoice();
        determiningOutcome(userChoice, computerChoice);
        repeatGame(userChoice, computerChoice);

    }

    public static String generateComputerChoice() {
        String computerChoice = "";
        int computerIntChoice;

        Random generator = new Random();
        computerIntChoice = generator.nextInt(3) + 1;

        if (computerIntChoice == 1) {
            computerChoice = "R";
        } else if (computerIntChoice == 2) {
            computerChoice = "P";
        } else if (computerIntChoice == 3) {
            computerChoice = "S";
        }
        System.out.println("The computer played "  + computerChoice);
        return computerChoice;

    }

    public static String generateUserChoice() {
        String userChoice;
        Scanner input = new Scanner(System.in);
        System.out.println("Please Enter your choice:");
        userChoice = input.nextLine();
        userChoice = userChoice.toUpperCase();

        return userChoice;

    }

    public static void determiningOutcome(String userChoice, String computerChoice) {

        if (userChoice.equals(computerChoice)) {
            System.out.println("It is a tie!");
        } else if (userChoice.equalsIgnoreCase("R") && computerChoice.equalsIgnoreCase("S")) {
            System.out.println("Rock beats Scissors, you win!!");
        } else if (userChoice.equalsIgnoreCase("P") && computerChoice.equalsIgnoreCase("R")) {
            System.out.println("Paper beats Rock, you win!!");
        } else if (userChoice.equalsIgnoreCase("S") && computerChoice.equalsIgnoreCase("P")) {
            System.out.println("Scissors beats Paper, you win!!");
        } else if (computerChoice.equalsIgnoreCase("R") && userChoice.equalsIgnoreCase("S")) {
            System.out.println("Rock beats Scissors, you lose.");
        } else if (computerChoice.equalsIgnoreCase("P") && userChoice.equalsIgnoreCase("R")) {
            System.out.println("Paper beats Rock, you lose.");
        } else if (computerChoice.equalsIgnoreCase("S") && userChoice.equalsIgnoreCase("P")) {
            System.out.println("Scissors beats Paper, you lose.");
        } else {
            System.out.println("Sorry, invalid choice.");
        }
    }

    public static int repeatGame(String userChoice, String computerChoice) {
        int playAgain;

        Scanner input = new Scanner(System.in);
             System.out.println("Would you like to play again? 1 = yes and 2 = no");
        playAgain = input.nextInt();

        if (playAgain == 1) {
       System.out.println("Would you like to play again? 1 = yes and 2 = no");
            generateUserChoice();
            generateComputerChoice();
            determiningOutcome(userChoice, computerChoice);

        }else {
            System.out.println("Thank you for playing!!");
        }

    return playAgain;    
    }

    }

但是我认为问题出在我的代码的generateComputerChoice部分

提前致谢

1 个答案:

答案 0 :(得分:1)

问题在于userChoicecomputerChoice一直是空的,因此它始终是平局。

您需要从方法的返回值中分配userChoicecomputerChoice

像这样:

userChoice = generateUserChoice();
computerChoice = generateComputerChoice();