Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Norm

Thread: Rock Paper Scissors Code...

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

    Default Rock Paper Scissors Code...

    Java Rock Paper Scissors code..?
    I need to create a simple rock paper scissors program using java, but without a computer player.
    Just player 1 inputs R, P or S, and then player 2 inputs R, P, or S. Then it gives a result on who won and who lost. Can anyone help?? I am very new to java.
    I know that I don't need to import Random because there is no computer player. And i need to import Scanner to take their inputs.

    I'm very bad with java language, I don't know how to take people's string inputs (though i do know how to take integers and doubles), and i need to write a while loop that allows it to keep asking them to retype R, P, or S, if they type something other than those three letters. Also, how would I write the if else statements for the actual processing of their inputs? Like, figuring out who wins?

    We haven't gotten to methods yet either... And we're only using the Scanner.

    Additional Details::

    import java.util.Scanner;
    public class RockPaperScissors {
    public static void main (String [] args){
    Scanner cool = new Scanner(System.in);
    System.out.println("Hello Player 1, what is your name?")
    ____ player1 = cool.next();
    System.out.println(Player 1 + ", please enter your choice (P for paper, R for rock or S for scissors):");
    ____ player1choice = cool.next();

    System.out.println("Hello Player 2, what is your name?")
    ____ player2 = cool.next();
    System.out.println(Player 2 + ", please enter your choice (P for paper, R for rock or S for scissors):");
    ____ player2choice = cool.next();

    }
    }

    this is all i've got. I think i have to use "else if" statements but i've never even used them before...
    and i just realized i'm not getting "ints" so... how do i accept "R, P, or S?"

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

    Default Re: Rock Paper Scissors Code...

    I think you should start with a very simple program.
    Try writing a program to prompt a user for some input, read the input, print out the input and exit.
    When that works, change it to read two different types of input, like an int and a String.
    When that works , change it to use a loop and get input from the user more than one time.

    When you have those three programs working, then you should be able to work on your project.

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

    Default Re: Rock Paper Scissors Code...

    I did my research, but now... I have a problem with my while loop. It prompts player1 to select R, P, or S repeatedly regardless of input!


    import java.util.Scanner;
    public class RockPaperScissors2 {
    public static void main (String [] args){
    Scanner cool = new Scanner(System.in);
    String player1, player1choice, player2, player2choice;

    System.out.println("Hello Player 1, what is your name?");
    player1 = cool.next();

    do{
    System.out.println(player1 + ", please enter your choice (P for paper, R for rock or S for scissors): ");
    player1choice = cool.next();
    }
    while(!player1choice.equals("R") || !player1choice.equals("S") || !player1choice.equals("P"));

    System.out.println("Hello Player 2, what is your name?");
    player2 = cool.next();

    do{
    System.out.println(player2 + ", please enter your choice (P for paper, R for rock or S for scissors):");
    player2choice = cool.next();
    }while(!player2choice.equals("R") || !player2choice.equals("S") || !player2choice.equals("P"));

    if((player1choice.equals("R") && player2choice.equals("R")) || (player1choice.equals("S") && player2choice.equals("S")) || (player1choice.equals("P") && player2choice.equals("P")))
    System.out.println(player1choice+" versus " + player2choice + " no winner this time" );

    else if((player1choice.equals("S") && player2choice.equals("P")) || (player1choice.equals("P") && player2choice.equals("R")) || (player1choice.equals("R") && player2choice.equals("S")) )
    System.out.println(player1 + " wins because the " + player1choice + " beats the " + player2choice );

    else
    System.out.println(player2 + " wins because the " + player2choice + " beats the " + player1choice );


    }
    }

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

    Default Re: Rock Paper Scissors Code...

    I did my research, but now... I have a problem with my while loop. It prompts player1 to select R, P, or S repeatedly regardless of input!


    import java.util.Scanner;
    public class RockPaperScissors2 {
    public static void main (String [] args){
    Scanner cool = new Scanner(System.in);
    String player1, player1choice, player2, player2choice;

    System.out.println("Hello Player 1, what is your name?");
    player1 = cool.next();

    do{
    System.out.println(player1 + ", please enter your choice (P for paper, R for rock or S for scissors): ");
    player1choice = cool.next();
    }
    while(!player1choice.equals("R") || !player1choice.equals("S") || !player1choice.equals("P"));

    System.out.println("Hello Player 2, what is your name?");
    player2 = cool.next();

    do{
    System.out.println(player2 + ", please enter your choice (P for paper, R for rock or S for scissors):");
    player2choice = cool.next();
    }while(!player2choice.equals("R") || !player2choice.equals("S") || !player2choice.equals("P"));

    if((player1choice.equals("R") && player2choice.equals("R")) || (player1choice.equals("S") && player2choice.equals("S")) || (player1choice.equals("P") && player2choice.equals("P")))
    System.out.println(player1choice+" versus " + player2choice + " no winner this time" );

    else if((player1choice.equals("S") && player2choice.equals("P")) || (player1choice.equals("P") && player2choice.equals("R")) || (player1choice.equals("R") && player2choice.equals("S")) )
    System.out.println(player1 + " wins because the " + player1choice + " beats the " + player2choice );

    else
    System.out.println(player2 + " wins because the " + player2choice + " beats the " + player1choice );


    }
    }

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

    Default Re: Rock Paper Scissors Code...

    Please edit your post and wrap the code in code tags.
    Also you need to properly format the code with indentations for each nesting level within pairs of {}
    Your unformatted code is very hard to read.

    Also check if you need to have {} for the if statements. With your unformatted code it is impossible to tell what statements you think should be controlled by the if and what statements follow the if.
    For example:
    Java Code:
        if(ttest > 3) {
            //do something here
            // and maybe some more
        }else {
             // do this here
        }
        // this is after the if

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

    Default Re: Rock Paper Scissors Code...

    Nevermind i found many things wrong with this code, i am so confused.

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

    Default Re: Rock Paper Scissors Code...

    That is one reason I suggested that you start with a simple program and build up some knowledge.

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

    Default Re: Rock Paper Scissors Code...

    Darn it, i'll try to ask my professor but he only has 15 minutes of office hours.

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

    Default Re: Rock Paper Scissors Code...

    I suggest that you start at the beginning and work through some simple examples and then come back to this program.
    Work on one thing at a time.
    Pick one of your current problems and work on that.
    DarrylBurke likes this.

Similar Threads

  1. Rock paper scissors.
    By katie_gsu in forum New To Java
    Replies: 7
    Last Post: 11-29-2011, 03:22 AM
  2. Rock, paper, scissors
    By StevenF in forum New To Java
    Replies: 3
    Last Post: 01-31-2011, 12:38 AM
  3. Rock Paper Scissors
    By 54byler in forum Advanced Java
    Replies: 2
    Last Post: 04-23-2009, 07:23 AM
  4. Need help with Rock Paper Scissors Game
    By GettinGwap in forum New To Java
    Replies: 12
    Last Post: 10-19-2008, 07:15 PM

Posting Permissions

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