Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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);
    }




    }
    }

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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!!");
    					}
    				}
    			}
    I am the luckiest man in the world


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •