JayLo
JayLo

Reputation: 63

when the user chooses to stop,how print the number of user wins, losses, and ties

At that point, the program reveals both choices and prints a statement indicating if the user won, the computer won,or if it was a tie.Continue playing until the user chooses to stop,then print the number of user wins, losses, and ties.

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

public class RockPaperScissor {

    public static void main(String[] args) {

        final int MAX = 3;
        int randomNumber;
        int userChoice;
        int computerChoice;
        int choice;
        Scanner input = new Scanner(System.in);
        Random rand = new Random();//random number generated by computer

        String str, another = "y";

        Scanner scan = new Scanner(System.in);
        int rounds = 0;
        userChoice = 0;

            while (another.equalsIgnoreCase("y")) {// allows y or Y
                System.out.print("enter 1= rock 2= paper 3 = scissor(0 to quit):");
                rounds++;
                userChoice = input.nextInt();
                computerChoice = rand.nextInt(MAX) + 1;
                System.out.println("computer picked " + computerChoice);

                int lose = 0;
                int win = 0;
                int tie = 0;
                int computerScore = 0, userScore = 0;


                if (userChoice == 1 || userChoice == 2 || userChoice == 3) {
                    if (userChoice == computerChoice) {
                        System.out.println("Tie Game!");
                        System.out.println();
                        tie++;
                        //rounds++;
                    } else if (userChoice == 1) {

                        if (computerChoice == 2) {
                            //rock is covered by paper
                            System.out.println("  you lose");
                            lose++;
                            computerScore++;
                            //rounds++;
                        } else if (computerChoice == 3) {
                            //rock smashes scissors
                            System.out.println("  you win");
                            win++;
                            userScore++;
                            //rounds++;
                        }
                    } else if (userChoice == 2) {
                        if (computerChoice == 3) {
                            //paper is cut by scissors
                            System.out.println("  you lose");
                            lose++;
                            computerScore++;
                            //rounds++;
                        } else if (computerChoice == 1) {
                            //paper covers rock
                            System.out.println("  you win");
                            win++;
                            userScore++;
                            //rounds++;
                        }
                    } else if (userChoice == 3) {

                        if (computerChoice == 1) {
                            //scissors are smashed by rock
                            System.out.println("  you lose");
                            lose++;
                            computerScore++;
                            //rounds++;
                        } else if (computerChoice == 2) {
                            //scissors cut paper
                            System.out.println("  you win");
                            win++;
                            userScore++;
                            //rounds++;
                        }
                    }

                } else {
                    System.out.println("invalid number");
                }
                System.out.println();
                System.out.println("User wins: " + win + " User loses: " + lose + "User ties " + tie);
                System.out.print("Do you want to play again (y/n)? ");
                another = scan.nextLine();
            }
           }
}

Upvotes: 1

Views: 336

Answers (3)

Dhanushka Gayashan
Dhanushka Gayashan

Reputation: 724

Go through the following code,

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

public class RockPaperScissor {

 public static void main(String[] args) {

    final int MAX = 3;
    int randomNumber = 0;
    int userChoice = 0;
    int computerChoice = 0;
    int choice = 0;
    int rounds = 0;
    int lose = 0;
    int win = 0;
    int tie = 0;
    int computerScore = 0;
    int userScore = 0;
    Scanner input = new Scanner(System.in);
    Random rand = new Random();//random number generated by computer
    String str, another = "y";

    userChoice = 0;

    while (another.equalsIgnoreCase("y")) {// allows y or Y
        System.out.print("enter 1= rock 2= paper 3 = scissor(0 to quit):");
        rounds++;
        userChoice = Integer.parseInt(input.nextLine());

        if(userChoice == 0){
            System.out.print("Do you want to play again (y/n)? ");
            another = input.nextLine();
        } else if (userChoice > 0 && userChoice < 4){
            computerChoice = rand.nextInt(MAX) + 1;
            System.out.println("computer picked " + computerChoice);

            if (userChoice == computerChoice) {
                System.out.println("Tie Game!");
                System.out.println();
                tie++;
            } else if (userChoice == 1) {

                if (computerChoice == 2) {
                    //rock is covered by paper
                    System.out.println("  you lose");
                    lose++;
                    computerScore++;
                } else if (computerChoice == 3) {
                    //rock smashes scissors
                    System.out.println("  you win");
                    win++;
                    userScore++;
                }
            } else if (userChoice == 2) {
                if (computerChoice == 3) {
                    //paper is cut by scissors
                    System.out.println("  you lose");
                    lose++;
                    computerScore++;
                } else if (computerChoice == 1) {
                    //paper covers rock
                    System.out.println("  you win");
                    win++;
                    userScore++;
                }
            } else if (userChoice == 3) {

                if (computerChoice == 1) {
                    //scissors are smashed by rock
                    System.out.println("  you lose");
                    lose++;
                    computerScore++;
                } else if (computerChoice == 2) {
                    //scissors cut paper
                    System.out.println("  you win");
                    win++;
                    userScore++;
                }
            }
        }else{
            System.out.println("invalid number");
        }
        System.out.println("computer score: " + computerScore + " You Score: " + userScore + " Rounds: " + rounds+"\n");
    }
 }

}

I had remove scan and one while loop from your original code and define the existing condition.

Good Luck !!!

Upvotes: 1

Daniel Ribeiro Moreira
Daniel Ribeiro Moreira

Reputation: 407

Your program seems fine, except for these points:

  • indentation is important!
  • you have a while(true) { /* do stuff */ break; } that is pointless.
  • scan and input are redundant. you may use any of them to read all user input.
    • they're equal, both of them are Scanner.new(System.in)

Upvotes: 1

Ashik Ali
Ashik Ali

Reputation: 11

You are infinite loop without Boolean condition

Firstly declare a field Boolean flag and put its initial value to false then when you will start game then set flag value to true and run while loop untill flag is true when you want to stop then simply set flag to false

Upvotes: 1

Related Questions