代码如下。我的程序运行顺利,除了两个问题。它不仅仅玩三回合,我需要玩三回合,并根据在三回合中至少有过一次平局的人(如果有平局)来确定获胜者(如果随机考虑这种情况)。直到玩家或计算机的赢率达到2比1为止。我不希望它像这样连续运行。另一个问题是,一旦宣布了最终的获胜者,“输入石头,纸张或剪刀”行将再次出现,并且在那里不需要。我该如何解决这两个问题?谢谢。

import java.util.Scanner;
import java.util.Random;
/**
 *
 * @author Chloe Harris
 *
 */
public class RockPaperScissorsGame {

    public static void main(String[] args) {
        // TODO code application logic here

        //Prompt user to enter rock, paper, or scissors
        //Compare random value
        while(true){
        int wins = 0;
        int losses = 0;
        int rnd = 0;
        int USER = 0;

        System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n"
            + "Enter \"Rock\", \"Paper\" or \"Scissors\" \n");


        // Plays 3 rounds before terminating
        while(rnd<3) {

            Random GAME = new Random();
            int PC = 1+GAME.nextInt(3);

            Scanner keyboard = new Scanner (System.in);
            int SCISSOR, ROCK, PAPER;
            ROCK = 1;
            PAPER = 2;
            SCISSOR= 3;

            String USER_Input =  keyboard.next();
            if(USER_Input.equals("Rock"))  {
                   USER = 1;
            }
            if(USER_Input.equals("Paper")) {
                   USER = 2;
            }
            if(USER_Input.equals("Scissors")) {
                   USER = 3;
            }



            //If the user enters a value greater then 3 or less than 1 it will terminate the program
            //and display an error message
            while (USER > 3 || USER < 1) {
                System.err.println("Incorrect value entered.");
                System.exit(0);
                break;
            }

            if(USER == PC){
                if(USER == SCISSOR){
                    System.out.println("Scissors v Scissors! Tie!");

            }
                if(USER == ROCK){
                    System.out.println("Rock v Rock! Tie!");
            }
                if(USER == PAPER){
                    System.out.println("Paper v Paper! Tie!");
            }
                System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
            }
            //Player wins

            if(USER == SCISSOR)
                if(PC == PAPER){
                System.out.println("Scissors v Paper! Player Wins!");
                wins++;
                rnd++;
                System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
            }
            //Computer wins
            else if(PC == ROCK){
                System.out.println("Scissors v Rock! Computer Wins!");
                losses++;
                rnd++;
                System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
            }
            //Player wins

            if(USER == ROCK)
                if(PC == SCISSOR ){
                System.out.println("Rock v Scissor! Player Wins!");
                wins++;
                rnd++;
                System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
            }
            //Computer wins
            else if (PC == PAPER){
                System.out.println("Rock v Paper! Computer Wins!");
                losses++;
                rnd++;
                System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
            }

            //Player Wins
            if(USER == PAPER)
                if(PC == ROCK){
                System.out.println("Paper v Rock! Player Wins!");
                wins++;
                rnd++;
                System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
                }
            //Computer Wins
            else if (PC == SCISSOR){
                System.out.println("Paper v Scissors! Computer Wins!");
                losses++;
                rnd++;
                System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
            }

            }

            if(wins>losses){
                System.out.println("The Player Wins!");
            }if(losses>wins){
                System.out.println("The Computer Wins!");
            }
            System.out.println("Play again? \"Yes\" or \"No\"");
            Scanner YN = new Scanner(System.in);

            String YN_String = YN.next();
            if(YN_String.equals("Yes") || YN_String.equals("yes")){
            }if(YN_String.equals("No") || YN_String.equals("no")) {
                System.out.println ("Goodbye!");
                break;
            }

        }
    }

}

最佳答案

该代码看起来应该可以按照您希望的方式工作,除了在平局的情况下,您不会增加舍入计数器:

        if(USER == PC){
            if(USER == SCISSOR){
                System.out.println("Scissors v Scissors! Tie!");
                rnd++;
            }
            if(USER == ROCK){
                System.out.println("Rock v Rock! Tie!");
                rnd++;
            }
            if(USER == PAPER){
                System.out.println("Paper v Paper! Tie!");
                rnd++;
            }
            System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
            System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
        }

附带说明一下,您可以从每个单独的结果中取出rnd++;,并在每次从用户那里得到输入时都增加ojit_code,因为无论如何它都希望它增加。

第二个问题是由于您在告诉用户结果(包括最终结果)后,每次收到输入时都会提示您输入新输入。您可以通过从各个结果中删除提示命令并将其放置在while循环的开始处来解决此问题。请注意,如果执行此操作,您可能还希望从欢迎消息中删除该提示,否则它将在开始时提示两次:一次在欢迎消息中,然后在while循环立即开始时再次提示。

关于java - 剪刀石头布游戏最佳2之3,带回圈,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32576797/

10-10 18:29