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

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);


      }

     }

最佳答案

然后决定我是赢了,被绑还是输了,但不会
  之后“重申”。之后,它只是说我要么赢了,要么输了,
  或连续绑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();
            /**/
        }

关于java - 剪刀石头布。 Java。方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19960797/

10-12 03:57