简体   繁体   中英

Java: Do-while loop with multiple conditions

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.

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.

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. "

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

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