6 Replies - 1668 Views - Last Post: 08 December 2011 - 09:00 PM Rate Topic: -----

#1 lytwynk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 22-November 11

RockPaperScissors program, need loop to continue until invalid entry?

Posted 08 December 2011 - 08:11 PM

Hello, I have written a rock, paper, scissors program and I want it to keep running and counting ties, user wins and computer wins until an invalid input is entered by the user... At the moment it runs three times unless there is an invalid entry, then the program is stopped. Any help would be much appreciated. Here is my program:

import java.util.Scanner;

public class RockPaperScissors
{
	public static void main(String[] args)
	{
		String userPlay;
		String compPlay;
		int computerInt;
		int gamesPlayed;
		int compWins = 0;
		int userWins = 0;
		int tieCount = 0;
		
		Scanner in = new Scanner(System.in);
		
		// Need to be able to play multiple times ?????????????????????????????????
		for(gamesPlayed = 0; gamesPlayed < 3; gamesPlayed++)
		{
		   System.out.println("User enter R for rock, P for paper or S for scissors.");
		   userPlay = in.nextLine();
		   userPlay = userPlay.toUpperCase();
		
			if(userPlay.equals("R") || userPlay.equals("P") || userPlay.equals("S"))
			{
				computerInt = (int)(Math.random()*3);
		
				switch (computerInt)
				{
					case 0: compPlay = "R";
						break;
					case 1: compPlay = "P";
				 	        break;
		   		        case 2: compPlay = "S";
				   	        break;
					default: compPlay = "nothing. Computer forfeits.";
				}
		
				System.out.println("Computer plays " + compPlay + '.');
		
				if(userPlay.equals(compPlay))
				{
					tieCount++;
				}
		
				if(userPlay.equals("R"))
				{
					if(compPlay.equals("S"))
					{
						userWins++;
					}
					else if (compPlay.equals("P"))
					{
						compWins++;
					}
				}
		
				if(userPlay.equals("P"))
				{
					if(compPlay.equals("S"))
					{
						compWins++;
					}
					else if (compPlay.equals("R"))
					{
						userWins++;
					}
				}
		
				if(userPlay.equals("S"))
				{
					if(compPlay.equals("R"))
					{
						compWins++;
					}
					else if(compPlay.equals("P"))
					{
						userWins++;
					}
				}
			}
		
			else
			{
				System.out.println("Invalid entry. game ended.");
				System.out.println("Final score:");
				System.out.println("Number of ties = " + tieCount);
			        System.out.println("User wins " + userWins + " times.");
			        System.out.println("Computer wins " + compWins + " times.");
			        break;
			}
		
			System.out.println();
			System.out.println("Number of ties = " + tieCount);
			System.out.println("User wins " + userWins + " times.");
			System.out.println("Computer wins " + compWins + " times.");
			System.out.println();
		}
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: RockPaperScissors program, need loop to continue until invalid entry?

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: RockPaperScissors program, need loop to continue until invalid entry?

Posted 08 December 2011 - 08:15 PM

You already have the idea, you just have to think bigger than just 3 times. :)
while( true ) { // go on forever with an infinite loop
// do your game code
// if the user enters invalid input, break out of the loop
break;
}

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: RockPaperScissors program, need loop to continue until invalid entry?

Posted 08 December 2011 - 08:34 PM

this is for 3 games

for(gamesPlayed = 0; gamesPlayed < 3; gamesPlayed++)

if you want infinite game just replace that line by

while(true)
Was This Post Helpful? 1
  • +
  • -

#4 lytwynk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 22-November 11

Re: RockPaperScissors program, need loop to continue until invalid entry?

Posted 08 December 2011 - 08:44 PM

Thanks so much! I did it like this and it is working!

boolean keepGoing = true;
while(keepGoing)

// game code.

// When user inputs invalid entry...

else
{
    keepGoing = false;
    break;
}


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: RockPaperScissors program, need loop to continue until invalid entry?

Posted 08 December 2011 - 08:49 PM

If you use a break; then the test is useless so 2 choices

boolean keepGoing = true;
while(keepGoing)

// game code.

// When user inputs invalid entry...

else
{
    keepGoing = false;
}


or
while(true) {

// game code.

// When user inputs invalid entry...

else
{
    break;
}


Was This Post Helpful? 1
  • +
  • -

#6 lytwynk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 22-November 11

Re: RockPaperScissors program, need loop to continue until invalid entry?

Posted 08 December 2011 - 08:59 PM

Ooo ok. I get it. Tanks so much for your help.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: RockPaperScissors program, need loop to continue until invalid entry?

Posted 08 December 2011 - 09:00 PM

Glad it's clearer :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1