Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-27-2008, 11:33 PM   PM User | #1
kaptinkow
New to the CF scene

 
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
kaptinkow is an unknown quantity at this point
help with rock paper scissors

it compiles fine, but when i run it, and press r,s, or p, it gives me more than what i want....does anyone know what's wrong?



import java.util.Scanner;

import java.util.Random;

public class Rock
{
//instance variables / properties
private static String personPlay;
private static String computerPlay;
private static int computerInt;

private static int c=0;
private static int p=0;
private static int t=0;
public static void main(String[] args)
{


Random generator = new Random();
Scanner kb = new Scanner(System.in);
//Get player's play -- note that this is stored as a string
for(int a=1;a<=10;a++){
System.out.println("Enter one of the following: \"R\"ock, \"P\"aper, or \"S\"cissors");
personPlay=kb.nextLine();



//Make player's play uppercase for ease of comparison
personPlay=personPlay.toUpperCase();

//Generate computer's play (0,1,2)
computerInt = generator.nextInt(3);


//Translate computer's randomly generated play to string
switch (computerInt){
case 0: computerPlay = "R";break;
case 1: computerPlay = "P";break;
case 2: computerPlay = "S";break;

}


System.out.println("Computer plays: " + computerPlay);






if (personPlay.equals(computerPlay))
{System.out.println("It's a tie!");
t++;}


if (personPlay.equals("R"))
{
{
if (computerPlay.equals("S"))
p++;
System.out.println("Rock crushes scissors. You win!!");
}
{
if (computerPlay.equals("P"))
c++;
System.out.println("Paper covers rock. Computer wins!!");
}
}



if(personPlay.equals("S"))
{
{
if (computerPlay.equals("R"))
System.out.println("Rock crushes scissors. Computer wins!!");
c++;
}
{
if (computerPlay.equals("P"))
p++;
System.out.println("Scissors shred paper. You win!!");
}
}

if(personPlay.equals("P"))
{
{
if (computerPlay.equals("S"))
System.out.println("Scissors shred paper. Computer wins!!");
c++;
}
{
if (computerPlay.equals("R"))
System.out.println("Paper covers rock. You win!!");
p++;
}
}


System.out.println("Computer: "+c+" Player: "+p+" Ties: "+t);
}




}
}
kaptinkow is offline   Reply With Quote
Old 10-28-2008, 08:00 AM   PM User | #2
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
Quote:
Originally Posted by kaptinkow View Post
it compiles fine, but when i run it, and press r,s, or p, it gives me more than what i want....does anyone know what's wrong?


Code:
import java.util.Scanner;

import java.util.Random;

public class Rock
{
   //instance variables / properties	
   private static String personPlay;   
	private static String computerPlay;  
	private static int computerInt;     
	                     
	private static int c=0;
	private static int p=0;
	private static int t=0;  
    public static void main(String[] args)
    {
	

	Random generator = new Random();
	Scanner kb = new Scanner(System.in);
	//Get player's play -- note that this is stored as a string
	for(int a=1;a<=10;a++){
	System.out.println("Enter one of the following: \"R\"ock, \"P\"aper, or \"S\"cissors");
	personPlay=kb.nextLine();
	
	

	//Make player's play uppercase for ease of comparison
	personPlay=personPlay.toUpperCase();

	//Generate computer's play (0,1,2)
	computerInt = generator.nextInt(3);
	

	//Translate computer's randomly generated play to string
	switch (computerInt){
	case 0: computerPlay = "R";break;
	case 1: computerPlay = "P";break;
	case 2: computerPlay = "S";break;

	}

	
	System.out.println("Computer plays: " + computerPlay);


	
	
	
	
	if (personPlay.equals(computerPlay))  
	{System.out.println("It's a tie!");
	t++;}
		
	
	if (personPlay.equals("R"))
	{
	{
	if (computerPlay.equals("S"))
	p++;
	System.out.println("Rock crushes scissors. You win!!");
	}
	{
	if (computerPlay.equals("P"))
	c++;
	System.out.println("Paper covers rock. Computer wins!!");
	}
	}
	

	
	if(personPlay.equals("S"))
	{
	{
	if (computerPlay.equals("R"))
	System.out.println("Rock crushes scissors. Computer wins!!");
	c++;
	}
	{
	if (computerPlay.equals("P"))
	p++;
	System.out.println("Scissors shred paper. You win!!");
	}
	}
	
	if(personPlay.equals("P"))	{
	{
	if (computerPlay.equals("S"))
	System.out.println("Scissors shred paper. Computer wins!!");
	c++;
	}
	{
	if (computerPlay.equals("R"))
	System.out.println("Paper covers rock. You win!!");
	p++;
	}
	}
	
	
	System.out.println("Computer: "+c+" Player: "+p+" Ties: "+t);
	}
	
		

    
}
}
the red if statements don't have opening and closing curlys. So the only code which is conditionally executed, is the line directly after the if statement. I think you want also the System.out.println to be conditionally executed.
Do these if statements like:
Code:
			if (personPlay.equals("R")) {
				{
					if (computerPlay.equals("S"))
					{	p++;
						System.out.println("Rock crushes scissors. You win!!");
					}
				}
				{
					if (computerPlay.equals("P"))
					{	c++;
					System.out.println("Paper covers rock. Computer wins!!");
					}
				}
			}
Roelf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:34 AM.


Advertisement
Log in to turn off these ads.