Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从石头剪纸代码(java)获取结果时遇到问题_Java_Methods - Fatal编程技术网

从石头剪纸代码(java)获取结果时遇到问题

从石头剪纸代码(java)获取结果时遇到问题,java,methods,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

所以当我运行我的代码时,它总是说这是一个平局,因为我认为我的一个方法有问题

这是我的全部代码:

   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部分


提前感谢

问题是
用户选择
计算机选择
总是空的,因此总是平局

您需要根据方法的返回值分配
userChoice
computerChoice

像这样:

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

您可以打印计算机选项。它打印什么?您没有分配选择变量(
userChoice
computerChoice
);它们仍然是空字符串。