Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Rock paper scissors game need help

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Rock paper scissors game need help

    Hello. I am trying to make a rock paper scissors program for a school project. I have the code is written and the main text parts etc works, However, the actual game does not work. The computer is supposed to choose between rock, paper and scissors, however, the user's choice is only printed and only asks if the user would like to play again. How do I make the computer choose one?

    // Code Written By Sherwin Gazor October 16 2020
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class GameRPS1
    {
      public static void main ( String[] args )
      {
     
        String getUserInput;
        Scanner sc =new Scanner(System.in); //Scanner gets created
        Random generator = new Random(); //Random gets created
        System.out.println("Welcome to Rock, Paper, Scissors! Do you want to play?"); // Welcome message
        getUserInput =sc.nextLine(); // Asks user if they would like to play
     
     
     
        while(getUserInput.equalsIgnoreCase("y"))//Removes case sensitives
        {
     
          int randomGenForPCChoice = generator.nextInt(3)+1; //Computer's chooses one of the three `
          System.out.println("Computer has made a choice. Make your choice!: ");
     
          System.out.println("Enter:Rock,Paper or Scissors");
          getUserInput=sc.nextLine(); //Asks you for choice``
          String getUserInput1=getUserInput.toUpperCase();
          char firstletter=getUserInput1.charAt(0);
     
          if(randomGenForPCChoice == 1) {
     
     
            System.out.println (" Computer chose rock!"); // If the computer chooses rock this will print
          }
     
          if(randomGenForPCChoice == 2) {
     
     
            System.out.println (" Computer chose paper!"); // If the computer chooses paper this will print
          }
     
           if(randomGenForPCChoice == 3) {
     
     
            System.out.println (" Computer chose scissors!"); // If the computer chooses scissors this will print
          }
     
           if 
          switch(firstletter) {
            case 'R':
              System.out.println(" You chose rock! ");
              break;
            case 'P':
              System.out.println(" You chose paper! ");
              break;
            case 'S':
              System.out.println(" You chose scissors! ");
     
              break;
     
          }
     
          System.out.println("Enter Y/y if you want to continue,otherwise N/n to stop the game");
          getUserInput =sc.nextLine(); // If you type this the game will stop
     
        }
        System.out.println("Game over! Thanks for playing!"); // Message appears after you quit
      }
    }

    Console :
    Welcome to rock paper scissors! Do you want to play?
    Y // Yes
    Computer made a choice. Make your choice:
    Enter:Rock,Paper or Scissors
    Rock // I choose rock
    You chose Rock // Only prints that I chose rock
    // Does not print what the computer chose and thus I can not make it so that if the computer chose X then X will happen.
    Enter Y/y if you want to continue,otherwise N/n to stop the game

    4 warnings found:
    File: C:\Users\Joe\Desktop\Computer Science\GameRPS1.java [line: 11]
    Warning: The local variable rock is never read
    File: C:\Users\Joe\Desktop\Computer Science\GameRPS1.java [line: 11]
    Warning: The local variable paper is never read
    File: C:\Users\Joe\Desktop\Computer Science\GameRPS1.java [line: 11]
    Warning: The local variable scissors is never read
    File: C:\Users\Joe\Desktop\Computer Science\GameRPS1.java [line: 13]
    Warning: The local variable randomGenForPCChoice is never read
    Last edited by shorba14; October 16th, 2020 at 05:09 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rock paper scissors game need help

    Please copy the full contents of the console window that shows what you are talking about and paste it here.

    Also Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Make sure the code has proper indentations for nested statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rock paper scissors game need help

    whenever i try to format it it just goes back to the way it looks now

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rock paper scissors game need help

    Did you try to add the code tags?
    http://www.java-forums.org/misc.php?do=bbcode#code

    Where is the contents of the console from when you execute the program?
    What you posted looks the the report from the compiler, not from when the program is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rock paper scissors game need help

    oh i got it now

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rock paper scissors game need help

    OK, that looks better. Can you add some comments to the console's contents to show where the program is not doing what you want?
    For example:
    Computer made a choice. Make your choice:
    Enter:Rock,Paper or Scissors
    Rock
    Rock <<<<<< This should be Paper
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rock paper scissors game need help

    done

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rock paper scissors game need help

    Does not print what the computer chose
    Where is the statement in the code that you expect should print the computer's choice?
    If there isn't one, then can you add a statement to print the computer's choice?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rock paper scissors game need help

    I added System.out.println (randomGenForPCChoice);
    However now I need to assign each number to each choice. After I can use if else statements to print something when something happens. For example, paper beats rock so if I choose paper and pc chooses rock, it will print that I won. If pc chooses paper and I pick rock, it will print that I lost. Same thing goes for ties. Is this correct?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rock paper scissors game need help

    That looks like a reasonable approach.
    One way to get the same values to compare would be to have a 3 element array of Strings: rock, paper and scissors and use a random number to index into the array to get the computer's choice as a String instead of as a number.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rock paper scissors game need help

    i will try that

    --- Update ---

    im having trouble doing that. however thank you for the help.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rock paper scissors game need help

    Post the new code and any error messages you get if you need help.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rock paper scissors game need help

    i just dont know how to use arrays like how you said since i learnt about it just yesterday

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rock paper scissors game need help

    Ok, instead of an array, use a switch statement or a chain of if/else if statements to convert the random number to a String that can be compared to the user's input.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rock paper scissors game need help

    hello sir sorry for the late reply. i got busy and had a class. i am almost done with the code and am basically 90% done. i will post the updated code.

    --- Update ---

    the only issues i have now is putting the choices in an if statement to make a decision. for example, if I pick rock and the computer pick scissors, it must type that I have won. i am having trouble doing that. this is my final request if you do not mind

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rock paper scissors game need help

    Where does the code compare the computer's choice against the user's choice to determine if there is a tie or a winner?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Oct 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with program.

    Hello. I am trying to make a rock paper scissors program for a school project. My code is 99% done however the 1 percent missing are the return messages for each game. For example, if I choose rock and pc chooses paper, it should say I lost. I wrote the code for it to perform that however, it does not. There are no errors when compiling.

    import java.util.Scanner; //Imports scanner
     
    import java.util.Random;//Improts random generator
     
    public class GameRPS1 
    {
      public static void main ( String[] args )
      {
     
        String getUserInput;//Creates a string for the users input
        Scanner sc =new Scanner(System.in); //Scanner gets created
        Random generator = new Random(); //Random gets created
        System.out.println("Welcome to Rock, Paper, Scissors! Do you want to play?"); // Welcome message
        getUserInput =sc.nextLine(); // Asks user if they would like to play
     
     
     
        while(getUserInput.equalsIgnoreCase("y"))//Removes case sensitives
        {
     
          int randomGenForPCChoice = generator.nextInt(3)+1; //Computer's chooses one of the three `
          System.out.println("Computer has made a choice. Make your choice!: ");
     
          System.out.println("Enter:Rock,Paper or Scissors");
          getUserInput=sc.nextLine(); //Asks you for choice``
          String getUserInput1=getUserInput.toUpperCase();
          char firstletter=getUserInput1.charAt(0);
     
          if(randomGenForPCChoice == 1) {
     
     
            System.out.println (" Computer chose rock!"); // If the computer chooses rock this will print
          }
     
          if(randomGenForPCChoice == 2) {
     
     
            System.out.println (" Computer chose paper!"); // If the computer chooses paper this will print
          }
     
           if(randomGenForPCChoice == 3) {
     
     
            System.out.println (" Computer chose scissors!"); // If the computer chooses scissors this will print
          }
     
           if(getUserInput.equals(1))  if(randomGenForPCChoice == 1){
     
             System.out.print("Tie!");
     
     
           }
            if(getUserInput.equals(2))  if(randomGenForPCChoice == 2){
             System.out.print("Tie!");
            }
     
     
            if(getUserInput.equals(3))  if(randomGenForPCChoice == 3){
             System.out.print("Tie!");
             }
     
            if(getUserInput.equals(1))  if(randomGenForPCChoice == 3){
             System.out.print("You win!");
            }
             if(getUserInput.equals(3))  if(randomGenForPCChoice == 2){
             System.out.print("You win!");
            }
               if(getUserInput.equals(1))  if(randomGenForPCChoice == 3){
             System.out.print("You win!");
            }
     
     
     
               if(getUserInput.equals(3))  if(randomGenForPCChoice == 1){   //Rock beats scissors
             System.out.print("You lose!");
            }
        if(getUserInput.equals(2))  if(randomGenForPCChoice == 3){   //Scissors beats paper
             System.out.print("You lose!");
            }
        if(getUserInput.equals(1))  if(randomGenForPCChoice == 2){  //Paper beats rock
             System.out.print("You lose!");
            }
     
     
     
          switch(firstletter) {
            case 'R':
              System.out.println(" You chose rock! "); //Prints if you choose rock
              break;
            case 'P':
              System.out.println(" You chose paper! ");//Prints if you choose paper
              break;
            case 'S':
              System.out.println(" You chose scissors! ");//Prints if you choose scissors
     
              break;
     
          }
     
     
     
          System.out.println("Enter Y/y if you want to continue,otherwise N/n to stop the game");
          getUserInput =sc.nextLine(); // If you type this the game will stop
     
        }
        System.out.println("Game over! Thanks for playing!"); // Message appears after you quit
      }
    }

    Welcome to Rock, Paper, Scissors! Do you want to play?
    Y
    Computer has made a choice. Make your choice!:
    Enter:Rock,Paper or Scissors
    Rock
    Computer chose rock!
    You chose rock!
    Enter Y/y if you want to continue,otherwise N/n to stop the game

    It should print "Tie!" but it does not.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rock paper scissors game need help

    What value is in getUserInput when this statement is executed?
           if(getUserInput.equals(1))  if(randomGenForPCChoice == 1){

    Add a print statement just before that statement to see.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Oct 2020
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rock paper scissors game need help

    thanks, but it prints all the statements now. also I'm sorry if it seemed I disregarded your post last time. its kinda hectic in my life right now and I wasn't patient

    --- Update ---

    ?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Rock paper scissors game need help

    What was printed as the value in getUserInput? Does it make sense to compare that to the int: 1.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with paper rock scissors game
    By sjud9227 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 24th, 2014, 07:46 PM
  2. ROCK, PAPER, SCISSORS GAME.
    By kofiachie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 21st, 2013, 07:24 AM
  3. Creating a GUI for a rock, paper, scissors game
    By nmg13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 11th, 2013, 02:09 PM
  4. Replies: 4
    Last Post: February 15th, 2013, 04:19 PM