how to stop a loop in java if a value is incremented consecutively for three times?

1.2k views Asked by At

i'm new to java. i'm writing a sample of rock paper scissors game. the user input 0,1,2 for rock, paper, scissors respectively. the program randomly generated the result. i almost managed to get it work but the only thing i'm stuck at is how to STOP THE FOR LOOP IF ONE OF THE SIDE WINS CONSECUTIVELY FOR THREE TIMES.

THIS IS THE ORIGINAL QUESTION

Write a program that plays scissor-rock-paper game. The rule is that a scissor wins against a paper, a rock wins against a scissor, and a paper wins against a rock. The program represent scissor as 0, rock as 1, and paper as 2. The game is to be played between the computer and a player. The program will prompt the user to enter the number of rounds to win. For example, if the user enter 4 then the winner has to win at least 3 out of the 4 rounds. If either party win 3 times consecutively, the game ends early without the fourth round. If the user enter 5 rounds, the winner has to win at least 3 out of the 5 rounds. The same rule of consecutive 3-wins also apply. After the user has entered the number of rounds, the computer randomly generates a number between 0 and 2. The program then prompts the user to enter a number 0, 1, or 2. After the last round (subject to the above mentioned early-winner-rule), the program display a message indicating whether the computer or the user wins, loses, or draws.

THIS IS MY CODE.

package assignment1;

import java.util.Scanner;
import java.util.Random;

public class question1_9 {

// This program is used to play scissor-rock-paper game.

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        Random random = new Random();
        int scissor = 0, rock = 1, paper = 2, round, userinput,comprand, userresult = 0, compresult = 0, control,j,k;

        // Variables used in this program is declared and initialized.

        /* Number of rounds wished to be play are obtained from user. */

        System.out.println("WELCOME TO ROCK PAPER SCISSOR GAME. ");
        System.out.println("PLEASE ENTER THE NUMBER OF ROUND YOU WANT TO PLAY:  ");
        round = scan.nextInt();
        control = (round/2)+1;

        for(int i = 0; i<round; i++)
        {
            if (compresult == control | userresult == control)
            {
                break;
            }
            System.out.println("ROUND " + (i+1));
            System.out.println("PLEASE ENTER:\n 0 for scissor \n 1 for rock \n 2 for paper  \n");
            userinput = scan.nextInt();
            comprand = random.nextInt(3);
            if (userinput == 0)
            {
                if (comprand == 0)
                {
                    System.out.println("COMPUTER IS SCISSOR");
                    System.out.println("DRAW!!!");
                    i--;
                }
                else if (comprand == 1)
                {
                    System.out.println("COMPUTER IS ROCK");
                    System.out.println("COMPUTER WINS!!!");
                    compresult++;
                }
                else
                {
                    System.out.println("COMPUTER IS PAPER");
                    System.out.println("YOU WIN!!!");
                    userresult++;
                }
                }
            else if (userinput == 1)
            {
                if (comprand == 0)
                {
                    System.out.println("COMPUTER IS SCISSOR");
                    System.out.println("COMPUTER WINS!!!");
                    compresult++;
                }
                else if (comprand == 1)
                {
                    System.out.println("COMPUTER IS ROCK");
                    System.out.println("YOU WIN!!!");
                    userresult++;
                }
                else
                {
                    System.out.println("COMPUTER IS PAPER");
                    System.out.println("DRAW!!!");
                    i--;
                }
                }
            else
            {
                if (comprand == 0)
                {
                    System.out.println("COMPUTER IS SCISSOR");
                    System.out.println("YOU WIN!!!");
                    userresult++;
                }
                else if (comprand == 1)
                {
                    System.out.println("COMPUTER IS ROCK");
                    System.out.println("DRAW!!!");
                    i--;
                }
                else
                {
                    System.out.println("COMPUTER IS PAPER");
                    System.out.println("COMPUTER WINS!!!");
                    compresult++;
                }
                }
            }
        if(compresult == userresult)
            System.out.println("\n\nFINAL RESULT IS DRAW!!!");
        else if (compresult > userresult)
            System.out.println("\n\nFINAL RESULT IS COMPUTER WIN!!!");
        else
            System.out.println("\n\nFINAL RESULT IS YOU WIN!!!");

        }

    }
2

There are 2 answers

2
shreyas On

use break; statement when you want to get out of current loop.

At top of loop,

store the wins in an array

String[] results=new String[rounds];

store your results as "user" or "comp" for each round and at the end of loop do this

if((results[i].equals("user") && results[i-1].equals("user") && results[i-2].equals("user") || (results[i].equals("comp") && results[i-1].equals("comp") && results[i-2].equals("comp")))
{
break;
}
1
0az On

Like shreyas said, use a break statement when you want to exit a loop.

I would set compresult to zero whenever the player wins a round, and set userresult to zero whenever the computer wins. Then, at the top of the loop, right after the { add:

if(userResult == 3 || computerResult == 3 || round/2 > 10)
{
    break;
}

Some code comes from shreyas's original post.