0

我假设这是一个逻辑错误。我无法将结果相加,也无法获得正确的结果。每次我输入石头、纸、剪刀时,它都会从那里决定我是赢、输还是平局。我的代码有什么问题?

public class RockPaperScissors {

        public static void displayGreeting()
        {
            String intro = "This program is a game.  A game of Rock, Paper, Scissors\n"+
                           "It is you against the computer.  Rock beats scissors, Paper\n"+
                           " beats rock, and scissors beats paper.  Good luck and may the\n"+
                           "odds be ever in your favor.";
            JOptionPane.showMessageDialog(null, intro, "Rock Paper Scissors",1);
        }

        public static String generateComputersChoice()
        {
            Random randomGenrator = new Random();
            int randomNumber = randomGenrator.nextInt(3);

            String weapon = "nothing";
            switch(randomNumber){
            case 0: weapon = "rock";
                break;
            case 1: weapon = "paper";
                break;
            case 2: weapon = "scissors";
                break;
            }
            return weapon;
            }

        public static String enterPlayersChoice(){

            String prompt = "You have a choice of picking rock, paper, or scissors.\n"+
                            "Choose wisely.";

            String input = "";

            input = JOptionPane.showInputDialog(null,prompt,"Choose your weapon",1);
            String inputLower = input.toLowerCase();
            return inputLower;

        }

        public static void main(String[] args)
        {
            displayGreeting();
          // generateComputersChoice();
           //enterPlayersChoice();
           // JOptionPane.showMessageDialog(null,generateComputersChoice()+ enterPlayersChoice(5));

            String player = enterPlayersChoice();
            String comp = generateComputersChoice();

            int ties = 0;
            int playerWins = 0;
            int compWins = 0;

            for(int i = 0; i < 3; i ++){



                //enterPlayersChoice(); //method

                //generateComputersChoice();  //method
                //JOptionPane.showMessageDialog(null,generateComputersChoice()+ enterPlayersChoice(1));

                //System.out.println(player+ " " + comp);
                //JOptionPane.showMessageDialog(null,player+ " " +comp);

                if(player.equals(comp)){
                    JOptionPane.showMessageDialog(null, "It's a tie!");
                    ties ++;

                }
                else if(player.equals("rock")){
                    if(comp.equals("scissors")){
                        JOptionPane.showMessageDialog(null, "You win!");
                        playerWins ++;

                    }
                }else if(comp.equals("rock")){
                    if(player.equals("scissors")){
                        JOptionPane.showMessageDialog(null, "You lose!");
                        compWins ++;

                    }
                }else if(player.equals("scissors")){
                    if(comp.equals("paper")){
                        JOptionPane.showMessageDialog(null, "You win!");
                        playerWins ++;

                    }
                }else if(comp.equals("scissors")){
                    if(player.equals("paper")){
                        JOptionPane.showMessageDialog(null, "You lose");
                        compWins ++;

                    }
                }else if(player.equals("paper")){
                    if(comp.equals("rock")){
                        JOptionPane.showMessageDialog(null, "You Win!");
                        playerWins ++;
                    }
                }else if(comp.equals("paper")){
                    if(player.equals("rock")){
                        JOptionPane.showMessageDialog(null, "You lose!");
                        compWins ++;
                    }
                }else{
                    JOptionPane.showMessageDialog(null, "Invalid user input");
                    i--;
                }


            }
          //Results
            JOptionPane.showMessageDialog(null,"Here are the results\n\n"+
                                               "\nTies: " +ties+
                                               "\nComputer Wins: " +compWins+
                                               "\nPlayer Wins: " + playerWins+
                                               "\n\n Program Terminating", "Results",1);


      }

     }
4

3 回答 3

2

然后它决定我是赢了、平了还是输了,但在那之后它不会“重复”。之后它只是说我连续 3 次赢、输或并列

你实际上在做的是询问输入一次然后运行循环,所以很明显你会得到相同的结果 3 次。

您需要在每次迭代时询问用户输入:

        String player;
        String comp;

        int ties = 0;
        int playerWins = 0;
        int compWins = 0;

        for(int i = 0; i < 3; i ++){
            player = enterPlayersChoice();
            comp = generateComputersChoice();
            /**/
        }
于 2013-11-13T17:52:45.313 回答
1

首先,您必须在每个循环中获得玩家的选择和计算机的选择。移动

String player = enterPlayersChoice();
String comp = generateComputersChoice();

开始时在for循环内。

另外,如果你说

else if(player.equals("rock")){
   if(comp.equals("scissors")){
       JOptionPane.showMessageDialog(null, "You win!");
        playerWins ++;
}

然后else if当玩家选择“摇滚”时,无论计算机的选择如何,该块都会匹配。那么,如果电脑不选择“剪刀”,什么都不会发生。

您需要两个条件都匹配才能执行此场景,否则第一个条件将匹配但第二个条件可能不匹配,并且不会发生任何事情。尝试

else if (player.equals("rock") && comp.equals("scissors")){

同样适用于其他条件。

于 2013-11-13T17:55:55.467 回答
0

您每次都通过循环重新生成用户和计算机的选择。移动

String player = enterPlayersChoice();
String comp = generateComputersChoice();

进入循环,否则3次都是一样的

于 2013-11-13T17:52:36.360 回答