首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >剪刀石头布游戏3中最好的2个循环问题

剪刀石头布游戏3中最好的2个循环问题
EN

Stack Overflow用户
提问于 2015-09-16 23:00:46
回答 3查看 1.1K关注 0票数 0

当输入了错误的输入(除了选项之外的某些东西;石头、布、剪刀)时,我如何让计算机自动获胜?它也需要计算这些数据。另外,当玩家选择再次玩游戏时,我如何删除"Welcome...“防止重新出现;它只在程序第一次启动时才需要。

代码语言:javascript
复制
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

        //Set integers for wins, losses, rounds, and user
        while(true){
            int wins = 0;
            int losses = 0;
            int round = 0;
            int Player = 0;

            //Plays 3 rounds before terminating
            //Prompt user to input Rock Paper Scissors
            System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");

            Scanner keyboard = new Scanner (System.in);
            while(round<3) {
                System.out.println("Enter \"Rock\", \"Paper\" or \"Scissors\"");
                Random Game = new Random();
                int Computer = 1+Game.nextInt(3);

                int Scissors, Rock, Paper;
                Rock = 1;
                Paper = 2;
                Scissors= 3;

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

                //If the user enters a value greater then 3 (Scissors) or less than 1 (Rock)
                //it will terminate the program and display an error message
                while (Player > 3 || Player < 1) {
                    losses++;
                    round++;
                    System.out.println("Not a valid input! Computer wins");
                    System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");

                }

                //Establish tie scenarios using if statements
                if(Player == Computer){
                    if(Player == Scissors){
                        System.out.println("Scissors v Scissors! Tie!");
                        round++;
                    }
                    if(Player == Rock){
                        System.out.println("Rock v Rock! Tie!");
                        round++;
                    }
                    if(Player == Paper){
                        System.out.println("Paper v Paper! Tie!");
                        round++;
                    }
                    System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");

                }

                //Establish the various winning scenarios using if and else if statements
                //Player wins
                if(Player == Scissors)
                    if(Computer == Paper){
                        System.out.println("Scissors v Paper! Player Wins!");
                        wins++;
                        round++;
                        System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                    }
                //Computer wins
                    else if(Computer == Rock){
                        System.out.println("Scissors v Rock! Computer Wins!");
                        losses++;
                        round++;
                        System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                    }
                //Player wins
                if(Player == Rock)
                    if(Computer == Scissors ){
                        System.out.println("Rock v Scissors! Player Wins!");
                        wins++;
                        round++;
                        System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                    }   
                //Computer wins
                    else if (Computer == Paper){
                        System.out.println("Rock v Paper! Computer Wins!");
                        losses++;
                        round++;
                        System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                    }
                //Player Wins
                if(Player == Paper)
                    if(Computer == Rock){
                        System.out.println("Paper v Rock! Player Wins!");
                        wins++;
                        round++;
                        System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");

                    }
                //Computer Wins 
                    else if (Computer == Scissors){
                        System.out.println("Paper v Scissors! Computer Wins!");
                        losses++;
                        round++;
                        System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
                    }
            }

            //Determine final winner using if statements
            //Ask if player would like to play again by setting up string 
            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 YesNo = new Scanner(System.in);

            String YesNo_String = YesNo.next();
            if(YesNo_String.equalsIgnoreCase("yes"))    {
            }if(YesNo_String.equalsIgnoreCase("No"))    {
                System.out.println ("Goodbye!");
            }
        }
    }
}
EN

回答 3

Stack Overflow用户

发布于 2015-09-16 23:11:38

您可以随时将if语句更改为以下内容:

代码语言:javascript
复制
if(UserInput.equals("Rock"))  {
    Player = 1;
}
else if(UserInput.equals("Paper")) {
    Player = 2;
}
else if(UserInput.equals("Scissors")) {
    Player = 3;
}
else {
    Player = 0;
}

您已经对其进行了设置,如果玩家< 1,计算机将自动获胜。

注意:您的while (Player > 3 || Player < 1)语句实际上应该是一条if语句。否则,您将在那里有一个无限循环。

要回答第二个问题,只需将语句System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");移到循环上方即可。这样它就只会被问一次:在开始的时候。

票数 1
EN

Stack Overflow用户

发布于 2015-09-16 23:11:52

对于玩家输入了错误的数据并检查他们是否已经玩过了,创建布尔值并在if/else语句中检查这些布尔值。由于这可能是作业,也可能不是作业,我将在“欢迎……”中解决这个问题。消息。

代码语言:javascript
复制
    public static void main(String[] args) {
    // TODO code application logic here
    Boolean firstPlay = true;

    //Set integers for wins, losses, rounds, and user
    while(true){
        int wins = 0;
        int losses = 0;
        int round = 0;
        int Player = 0;

        //Plays 3 rounds before terminating
        //Prompt user to input Rock Paper Scissors
        if(firstPlay) {
            System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");
            firstPlay = false;
        }
票数 0
EN

Stack Overflow用户

发布于 2015-09-16 23:13:20

似乎您正在将"Player“的值设置为1、2或3,其默认值为0。我会说,在每次循环开始时将其设置为默认值,如果它为0,则允许计算机获胜。或者将if satements更改为if-elseif if。下面我将提供一个示例。

代码语言:javascript
复制
....
if(UserInput.equals("Rock"))  {
    Player = 1;    
} else if(UserInput.equals("Paper")) {
   Player = 2;
} else if(UserInput.equals("Scissors")) {
   Player = 3;
} else {
    Player = 0;
} 

因为您已经有一个声明,如果玩家<1或>3表示计算机获胜,那么您就设置好了。你的"Welcome“语句也在while循环中。正因为如此,每次循环迭代时,你都会看到它。试着把它移到循环之外。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32612249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档