3

I am trying to create the scissors-paper-stone-game in Java with a do-while loop. The computer will randomly select 1, and the user makes his choice. The exit condition is if the user wins twice (userWin) or the computer wins twice (compWin). If there is a draw, neither counter increases.

// Scissors, paper, stone game. Best of 3.
// scissors = 0; paper = 1; stone = 2;
import java.util.Scanner;
public class Optional2 {

    public static void main(String[] args) {

            int userWin = 0;
            int compWin = 0;

            do {
                //comp choice
                int comp = 1;       //TEST CASE
            //  int comp = (int) (Math.random() * (2 - 0 + 1) + 0);

                //user choice
                System.out.println("0 for scissors, 1 for paper, 2 for stone");
                Scanner sc = new Scanner(System.in);
                int user = sc.nextInt();

                    //Draw
                    if (comp == user) {
                        System.out.println("Draw");
                    //Win =)
                    } else if (comp == 0 && user == 2 || comp == 1 && user == 0 || 
                                comp == 2 && user == 1) {
                        System.out.println("WIN!");
                        userWin++;
                    //Lose =(
                    } else {
                        System.out.println("Lose =(");
                        compWin++;
                    }
            } while (compWin < 2 || userWin < 2);

            System.out.println("You won " + userWin + " times!");   
    }
}

For int comp, it should be random, but I am setting it to 1 (paper) for easy testing.

However, presently only the 1st condition will exit the loop if it becomes true. I am expecting the 2nd condition to exit the loop too if it becomes true with the || operator, but the loop just keeps looping even if it comes true.

ie. if I put while (userWin < 2 || compWin < 2), it will exit if the user wins twice but not if the computer wins twice. If I put while (compWin < 2 || userWin < 2), it will exit if the computer wins twice but not if the user wins twice.

I tried changing it to while ((userWin < 2) || (compWin < 2)) too but it doesn't work.

4
  • replace ||, which means "or", by &&, which means "and" Commented Mar 3, 2016 at 16:17
  • While conditions aren't to control exiting the loop but to control staying in the loop. It's hard to rationalize if you don't see them for what they are. You're staying in the loop while either the computer or user have less than 2 wins, so if either of them gets two wins that's it. Commented Mar 3, 2016 at 16:17
  • Do you realize that you coded the condition for the while loop to stop only when BOTH compWin and userWin is 2 or larger? Commented Mar 3, 2016 at 16:17
  • couldn't you just handle the conditions inside the loop with if statements and toggle a boolean variable as your exit condition? Commented Mar 3, 2016 at 16:34

3 Answers 3

4

but the loop just keeps looping even if it comes true

A while loops keeps looping as long as the condition remains true.

I think the problem is that you should rewrite the condition to:

while ((userWin < 2) && (compWin < 2))

with && instead of ||. Indeed: now the while loop is something like: "Keep looping as long as the the user has not won two or more times, and the computer has not won two or more times."

Sign up to request clarification or add additional context in comments.

Comments

4

You should use && instead:

while (userWin < 2 && compWin < 2);

This is because you want to be in the loop as long as none of the user or comp gets 2 consecutive wins

That is translated into

userWin < 2 && (=AND) compWin < 2

Which means: as long as both the user AND the comp has less than 2 consecutive wins, stays in the loop.

Or in other words, as you have phrased it: if any of user or comp gets two consecutive wins, gets out from the loop.

Comments

0

Try replace with &&. You need both less that 2 to keep loop going on

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.