简体   繁体   中英

Why isn't my while loop reading both statements?

So, I have this code that is meant to play a simple rock, paper, scissors game using another computer move class and string input. This is for a school project so we are supposed to use while loops, but for some reason the loop doesn't seem to be evaluating both sides of the argument, and finishes the loop once the computer score reaches the user input (which is number - the user inputs the number they would like to play to). (PS: I'm super new to Java so I'm still trying to get the basics down:) )

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

  }

}

It is because you are using and or clause.

Using || (most common known as 'or') will evaluate the first expression if it is true, then (in java) won't evaluate the second one and enter the loop.

On the other hand, using && (most common known as 'and') will evaluate both side of the expression and if BOTH of them are true, then it will enter the loop.

In this case you want that neither the user nor the computer has not reach the 'number' in that case you should be using the &&

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM