Results 1 to 8 of 8
  1. #1
    Shaeman111 is offline Member
    Join Date
    Feb 2012
    Location
    Virginia
    Posts
    10
    Rep Power
    0

    Default java rock paper scissors code!!!

    So I managed to get some code going, but I can't seem to figure out how to get the if comparisons to work. It stores their inputs, and if it's a tie, it works. But for anything different it doesn't seem to work. How can I compare their inputs?


    Java Code:
    import java.util.Scanner;
    public class RockPaperScissors4 {
    public static void main (String [] args){
    Scanner cool = new Scanner(System.in);
    String player1; 
    char player1choice;
    String player2;
    char player2choice;
    System.out.println("Hello Player 1, what is your name?");
    player1 = cool.next();
    
    System.out.println(player1 + ", please enter your choice (R for rock, P for paper or S for scissors): ");
    player1choice = cool.next().charAt(0);
    
    	while(player1choice !='R' && player1choice !='P' && player1choice !='S' && player1choice !='r' && player1choice !='p' && player1choice !='s')
    	{
    	System.out.println("That is an incorrect input. Please enter a legitimate response");
    	player1choice = cool.next().charAt(0);
    	}
    	
    System.out.println("Hello Player 2, what is your name?");
    player2 = cool.next();
    
    System.out.println(player2 + ", please enter your choice (R for rock, P for paper or S for scissors): ");
    player2choice = cool.next().charAt(0);
    
    	while(player2choice !='R' && player2choice !='P' && player2choice !='S' && player2choice !='r' && player2choice !='p' && player2choice !='s')
    	{
    	System.out.println("That is an incorrect input. Please enter a legitimate response");
    	player2choice = cool.next().charAt(0);
    	}
    	char R = 0;
    	char S = 1;
    	char P = 2;
    	
    	if (player1choice == player2choice)
                System.out.println("Tie!");
            else if (player1choice == 0 && player2choice == 1)
                System.out.println("Rock beats scissors! You win!");
            else if (player1choice == 0 && player2choice == 2)
                System.out.println("Paper covers rock... You lose.");
            else if (player1choice == 1 && player2choice == 0)
                    System.out.println("Rock beats scissors... you lose.");
            else if (player1choice == 1 && player2choice == 2)
                    System.out.println("Scissors cuts paper! You win!");
            else if (player1choice == 2 && player2choice == 0)
                System.out.println("Paper covers rock! You win!");
            else if (player1choice == 2 && player2choice == 1)
                System.out.println("Scissors cuts paper... You lose.");
            else
                System.out.println();
             
    	
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: java rock paper scissors code!!!

    It looks like you are comparing a char value: player1choice against an int value: 1.
    The value of '1' is not equal to 1. Change your compares to be against char values, not int values

  3. #3
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: java rock paper scissors code!!!

    I do not understand your logic at all ... playerchoises will be Char and not an integer between 0 and 2 ... and the char R = 0; things that you are doing has no purpose , numbers can't be used as references in they way that you plan

    To correct this change the if statements in the end to char. Also some code is missing in the while loops.

  4. #4
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default Re: java rock paper scissors code!!!

    Norm beated me to it ... have to write faster =P

  5. #5
    Shaeman111 is offline Member
    Join Date
    Feb 2012
    Location
    Virginia
    Posts
    10
    Rep Power
    0

    Default Re: java rock paper scissors code!!!

    Okay... I really really need a visual of how i should do this, i tried to fix it and it only made it worse !

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: java rock paper scissors code!!!

    it only made it worse !
    You'll have to show us if you want help with it.

  7. #7
    Shaeman111 is offline Member
    Join Date
    Feb 2012
    Location
    Virginia
    Posts
    10
    Rep Power
    0

    Default Re: java rock paper scissors code!!!

    Oh, i tried a lot of things last night but i didn't save them because they didn't help. I guess i'll keep trying.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: java rock paper scissors code!!!

    If you have questions or problems, post them.

Similar Threads

  1. Rock Paper Scissors Code...
    By Shaeman111 in forum New To Java
    Replies: 8
    Last Post: 02-08-2012, 04:20 AM
  2. Rock paper scissors.
    By katie_gsu in forum New To Java
    Replies: 7
    Last Post: 11-29-2011, 03:22 AM
  3. Help! (Java GUI - Rock, Paper, Scissors)
    By Snowball in forum New To Java
    Replies: 5
    Last Post: 06-16-2011, 07:37 AM
  4. Rock Paper Scissors
    By 54byler in forum Advanced Java
    Replies: 2
    Last Post: 04-23-2009, 07:23 AM
  5. Need Help with Rock paper and Scissors Java Game
    By kingsun in forum New To Java
    Replies: 3
    Last Post: 11-17-2008, 04:35 AM

Posting Permissions

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