【问题标题】:Why isn't my while loop reading both statements?为什么我的 while 循环不读取这两个语句?
【发布时间】:2019-02-21 20:57:32
【问题描述】:

所以,我有这段代码,旨在使用另一台计算机移动类和字符串输入来玩简单的石头、纸、剪刀游戏。这是一个学校项目,所以我们应该使用 while 循环,但由于某种原因,循环似乎没有评估参数的双方,并且一旦计算机分数达到用户输入(即数字- 用户输入他们想玩的号码)。 (PS:我是 Java 的超级新手,所以我仍在努力学习基础知识:))

public class RockPaperScissors {

 public static void main(String[] args) {

   Scanner input = new Scanner(System.in);

        int playerScore, computerScore; // score
        playerScore = 0;
        computerScore = 0;

        System.out.println("Enter number of points to win:"); // number of rounds
        int number = input.nextInt();

        boolean PLAYER_VALID = true;

  while ((playerScore < number) || (computerScore < number)) {

    System.out.println("Choose Rock, Paper or Scissors:");
    String playerMove = input.next().toLowerCase();

    if (!(playerMove.equals("rock") // ensuring valid user input
                || playerMove.equals("paper")   
                || playerMove.equals("scissors"))) 
            {   
                PLAYER_VALID = false;
            }

        // computer move

        String computerMove = ComputerOpponent.getMove();

        // determining win

        if (playerMove.equals(computerMove)) 
            {   
                System.out.println("The computer chose " + playerMove + " so, it's a tie!");    
            }

        else if (playerMove.equals("rock")) 
            {           
                if (computerMove.equals("paper")) 
                {               
                    System.out.println("Computer chose paper, you lose :(");
                    computerScore += 1;
                }   
                else {
                    System.out.println("Computer chose scissors, you win :)");
                    playerScore += 1;
                }
            }

        else if (playerMove.equals("paper")) 
            {           
                if (computerMove.equals("scissors")) 
                {               
                    System.out.println("Computer chose scissors, you lose :(");
                    computerScore += 1;
                }   
                else {
                    System.out.println("Computer chose rock, you win :)");
                    playerScore += 1;
                }
            }

        else if (playerMove.equals("scissors")) 
            {           
                if (computerMove.equals("rock")) 
                {               
                    System.out.println("Computer chose rock, you lose :(");
                    computerScore += 1;
                }   
                else {
                    System.out.println("Computer chose paper, you win :)");
                    playerScore += 1;
                }
            }

        System.out.println("The current score is " 
        + "(" + playerScore + "," + computerScore + ")");

        }  // while - round

        if (computerScore == number) 
            {
                System.out.println("Computer wins. Better luck next time!");
            }
        else if (playerScore == number)
            {
                System.out.println("Congrats! You win!");
            }
        else
            System.out.println("Something happened....ERROR");

  }

}

【问题讨论】:

  • 输入变量是什么?
  • 我刚刚添加了上面的变量!如果您需要代码的任何其他部分,请告诉我。谢谢! :)
  • 我认为你应该在你的while循环中添加&amp;&amp;。如果两个分数都很低,那么数字不仅仅是进入循环内部。这有意义吗?
  • 当代码遇到 input.next() 时,它会阻塞或暂停以读取用户输入。
  • 哦,所以 ||是等到电脑和播放器都高于输入的数字吗?

标签: java loops while-loop


【解决方案1】:

这是因为你使用了and or 子句。

使用 || (最常见的称为“或”)如果第一个表达式为真,将评估第一个表达式,然后(在 java 中)不会评估第二个表达式并进入循环。

另一方面,使用 &&(最常见的是“和”)将计算表达式的两边,如果它们都为真,那么它将进入循环。

在这种情况下,您希望用户和计算机都没有达到“数字”,在这种情况下,您应该使用 &&

【讨论】:

  • 这是有道理的。代码现在可以工作了!非常感谢您的精彩解释!
猜你喜欢
  • 2015-06-12
  • 1970-01-01
  • 2019-07-07
  • 1970-01-01
  • 2018-05-03
  • 2011-12-03
  • 2018-03-17
  • 2016-02-19
  • 1970-01-01
相关资源
最近更新 更多