Java Rock Paper游戏循环太多次了

时间:2018-09-06 19:54:36

标签: java loops if-statement

我不得不做一个剪刀石头布游戏,在那儿你要在电脑上玩。 1,2,3用于石头,纸张,剪刀。它循环5次,并显示计算机获胜和玩家获胜的数量。除了循环之外,我已完成所有工作。当您输入大于3的数字时,假设您说“无效”并循环播放,直到您玩5个有效游戏为止。但是,如果您输入错误的答案数量奇异,它会使您玩6场游戏,而如果您输入错误的答案数量偶数,它将使您玩5场游戏。我需要它使您能够玩5场比赛,无论奇数或偶数错了。请帮忙。

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    int player = 0, computer = 0;
    int computerScore = 0, playerScore = 0;
    int loops = 0;

    int rock = 1;
    int paper = 2;
    int scissors = 3;

    for (int i = 1; i < 6; i++) {


        computer = (int) (Math.random() * 3) + 1;
        System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
        player = reader.nextInt();


        if (player > scissors) {
            System.out.println("Not a valid response");
            System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
            player = reader.nextInt();
            i--;

        }


        if (player == computer) {
            System.out.println("Tie");
        } else if (player == rock) {

            if (computer == paper) {
                System.out.println("Player picked Rock, Computer picked Paper, Computer wins");
                computerScore++;
            } else if (computer == scissors) {
                System.out.println("Player picked Rock, Computer picked Scissors, Player wins");
                playerScore++;
            }
        } else if (player == paper) {

            if (computer == rock) {
                System.out.println("Player picked Paper, Computer picked Rock , Player wins");
                playerScore++;
            } else if (computer == scissors) {
                System.out.println("Player picked Paper, Computer picked Scissors, Computer wins");
                computerScore++;
            }
        } else if (player == scissors) {
            if (computer == rock) {
                System.out.println("Player picked Scissors, Computer picked Rock , Computer wins");
                computerScore++;
            } else if (computer == paper) {
                System.out.println("Player picked Scissors, Computer picked Paper, Player wins");
                playerScore++;
            }

        }

    }
    System.out.println("");
    System.out.println("Computer Wins " + computerScore);
    System.out.println("Player Wins " + playerScore);

}

2 个答案:

答案 0 :(得分:1)

为了使事情变得容易,我建议使用while循环。

E.G:

func processString(_ s: String) ->Bool {
    var sum = 0
    for char in s{
        if let number = Int(String(char)) {
            // number is not nil
        }
    }
}

答案 1 :(得分:0)

您的代码逻辑不正确。我已经使用枚举Options对其进行了重组,以保存岩石,纸张和剪刀的值。我还使用了Player类,可以用作用户或计算机,或者将来用作其他播放器。

枚举可容纳岩石,纸张和剪刀的所有可能值

 enum Options {

        ROCK(1), PAPER(2), SCISSORS(3);

        private Options(int item) {
            this.item = item;
        }

        private final int item;

        public int getItem() {
            return item;
        }

    }

将Class Player设置为“用户”或“计算机”

class Player {
    private int score;
    private int input;

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getInput() {
        return input;
    }

    public void setInput(int input) {
        this.input = input;
    }

}

最后,主类带有其他一些实用方法

public class Game {

  private static int MAX_ATTEMPTS = 5;

  // main method
  public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    Player user = new Player();
    Player comp = new Player();
    int input = 0;

    while (MAX_ATTEMPTS > 0) {
      // reading input till it's right!
      while (true) {
        try {
           System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
           input = reader.nextInt();
           // valid only if its one of the above
           if (input == Options.PAPER.getItem() || input == Options.SCISSORS.getItem() || input == Options.ROCK.getItem()) {
              user.setInput(input);
              comp.setInput(fetchComputerInput());
              computeHigherHand(user, comp);
              MAX_ATTEMPTS--;
              break;
           } else {
              System.err.println("Invalid input");
              reader.next();
           }
         } catch (Exception e) {
            System.err.println("Invalid input");
            reader.next();
        }
      }
    }

    checkWinner(user, comp);
    reader.close();
  }
}

返回计算机分数的方法

    public static int fetchComputerInput() {
        return (int) (Math.random() * 3) + 1;
    }

确定较高手牌的方法

    public static void computeHigherHand(Player user, Player comp) {
        // when user enters 'rock'
        if (user.getInput() == Options.ROCK.getItem()) {
            // comp wins
            if (comp.getInput() == Options.PAPER.getItem()) {
                System.out.println("Player picked Rock, Computer picked Paper, Computer wins");
                comp.setScore(comp.getScore() + 1);
            }
            // user wins
            else if (comp.getInput() == Options.SCISSORS.getItem()) {
                System.out.println("Player picked Rock, Computer picked Scissors, Player wins");
                user.setScore(user.getScore() + 1);
            }
            // draw
            else {
                System.out.println("Player picked Rock, Computer picked Rock, Draw");
            }
        }

        // when user enters 'paper'
        if (user.getInput() == Options.PAPER.getItem()) {
            // user wins
            if (comp.getInput() == Options.ROCK.getItem()) {
                System.out.println("Player picked Paper, Computer picked Rock , Player wins");
                user.setScore(user.getScore() + 1);
            }
            // comp wins
            else if (comp.getInput() == Options.SCISSORS.getItem()) {
                System.out.println("Player picked Paper, Computer picked Scissors, Computer wins");
                comp.setScore(comp.getScore() + 1);
            }
            // draw
            else {
                System.out.println("Player picked Paper, Computer picked Paper, Draw");
            }
        }

        // when user enters 'scissors'
        if (user.getInput() == Options.SCISSORS.getItem()) {
            // comp wins
            if (comp.getInput() == Options.ROCK.getItem()) {
                System.out.println("Player picked Scissors, Computer picked Rock , Computer wins");
                comp.setScore(comp.getScore() + 1);
            }
            // user wins
            else if (comp.getInput() == Options.PAPER.getItem()) {
                user.setScore(user.getScore() + 1);
                System.out.println("Player picked Scissors, Computer picked Paper, Player wins");
            }
            // draw
            else {
                System.out.println("Player picked Scissors, Computer picked Scissors, Draw");
            }
        }

    }

确定获胜者的方法

    public static void checkWinner(Player user, Player comp) {
        // check who won
        if (user.getScore() > comp.getScore())
            System.out.println("User wins! User score : " + user.getScore() + " Comp score : " + comp.getScore());
        else if (user.getScore() < comp.getScore())
            System.out.println("Comp wins! User score : " + user.getScore() + " Comp score : " + comp.getScore());
        else
            System.out.println("Tie");
    }

让我知道是否需要进一步说明。