//****************************************************************************************************
//  Project15.java       Author: Lewis/Loftus/Cocking
//
//  Write an application that plays the rock-paper-scissors game against the computer. When played 
//  between two people, each person picks one of the three options at the same time and the winner is
//  determined. The program should randomly choose one of the three options. Keep playing until the 
// user choses to stop, then print the number of user wins, losses, and ties. 
//****************************************************************************************************

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

public class Project15
{
    //----------------------------------------------------------------------------------------------
    //  Generates random number and asks user to guess.
    //----------------------------------------------------------------------------------------------
    public static void main (String[] args)
    {
        Random generator = new Random();
        Scanner scan = new Scanner (System.in);
        String str, another = "y";

        System.out.println("Rock, Paper, or Scissors?");
        String choice = scan.nextLine();

        int win = 0, tie = 0, lose = 0, winN = 0, loseN = 0, tieN = 0;
        final int ROCK = 0, SCISSORS = 1, PAPER = 2; 
        int comp = generator.nextInt(3);

        while (choice.equalsIgnoreCase("rock"))
        {
            if (comp == 1)
            {
                System.out.println("Rock beats scissors. You win!");
                win++;
            }
            else if (comp == 2)
            {
                System.out.println("Paper beats rocks. You lose!");
                lose++;
            }
            else
            {
                System.out.println("Rock = Rock. Tie!");
                tie++;
            }

            if (comp == 0)
            {
                System.out.println("The computer's selection was Rock");
            }
            else if (comp == 1)
            {
                System.out.println("The computer's selection was Scissors");
            }
            else
            {
                System.out.println("The computer's selection was Paper");
            }  
            System.out.println("Play again? (y/n)");
            another = scan.nextLine();
        }

        while (choice.equalsIgnoreCase("paper"))
        {
            if (comp == 1)
            {
                System.out.println("Scissors beats paper. You lose!");
                lose++;
            }
            else if (comp == 0)
            {
                System.out.println("Paper beats rock. You win!");
                win++;
            }
            else
            {
                System.out.println("Paper = Paper. Tie!");
                tie++;
            }

            if (comp == 0)
            {
                System.out.println("The computer's selection was Rock");
            }
            else if (comp == 1)
            {
                System.out.println("The computer's selection was Scissors");
            }
            else
            {
                System.out.println("The computer's selection was Paper");
            }

            System.out.println("Play again? (y/n)");
            another = scan.nextLine();
        }

        while (choice.equalsIgnoreCase("scissor"))
        {
            if (comp == 2)
            {
                System.out.println("Scissors beats paper. You win!");
                win++;
            }
            else if (comp == 0)
            {
                System.out.println("Rock beats scissors. You lose!");
                lose++;
            }
            else
            {
                System.out.println("Scissors = scissors. Tie!");
                tie++;
            }

            if (comp == 0)
            {
                System.out.println("The computer's selection was Rock");
            }
            else if (comp == 1)
            {
                System.out.println("The computer's selection was Scissors");
            }
            else
            {
                System.out.println("The computer's selection was Paper");
            }
            System.out.println("Play again? (y/n)");
            another = scan.nextLine();
        }

        if (another.equalsIgnoreCase("n"))
        {

            winN += win;
            loseN += lose;
            tieN += tie;

            System.out.println("Number of wins: " + winN);
            System.out.println("Number of loss: " + loseN);
            System.out.println("Number of ties: " + tieN);

            System.out.println("Play again? (y/n)");
            another = scan.nextLine();

        }


    }
}

This is what I have so far.

But when I choose to play again, the command doesn't go through.

PLEASE HELP!!!!!

Recommended Answers

All 3 Replies

If you want to keep asking for playing again, you need another loop around it. Either let users get out of the loop by saying 'n' or not equal to 'y' is your choice. You already know how to use while-loop in your case. You could simply add another while-loop around the whole portion you want it to repeat again after a user enters 'y'.

I put the while loop around the Random generator until the end. It still gives me the same error. I know how to put the while loop but I'm not sure where!

The while-loop should start between line 29~30 and ends at line 150.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.