Hi guys, I'm working on a program which is essentially Rock Paper Scissors Vs. the computer.

Anyway I'm having a bit of difficulty with it and could use some assistance.

I need to use a class method, so I decided to separate the computer's choice in the class method, I'd then like to take that random choice and store it in computerPlay (line 27)

I'm not sure how to return the info in the class method.

Here's the code so far, any help is appreciated!

//RockPaperScissors.java
//

import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors

{
    // Prompts user to choose: rock,paper, or scissors
    // Computer pseudo-randomly picks rock, paper, or scissors
    // Prints out who won, keeps going until user wins
    // Prints out how many times it took for user to beat computer
    
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        Random generator = new Random();
        String personPlay; //User's choice - R, P, or S
        String computerPlay; // Computer's choice - R, P, or S
        int computerInt; // Random generated number to choose computer's choice
        
        
        System.out.println("Enter R for Rock, P for Paper, S for Scissors: ");
        personPlay = scan.nextLine();
        personPlay = personPlay.toUpperCase();
        
        computerPlay = pickHandShape();
    }
    
    System.out.println("Computer plays: "+ computerPlay);
    
        if (personPlay.equals(computerPlay))
        { 
            System.out.println("It's a tie!");
        }
         
        else if (personPlay.equals("R"))
        {
            if (computerPlay.equals("S"))
            System.out.println("Rock crushes scissors.  You win!!");
           else if (computerPlay.equals("P"))
            System.out.println ("Paper eats rock. You lose!!");
        }
        else if (personPlay.equals("P"))
        {
           if (computerPlay.equals("S"))
            System.out.println ("Scissor cuts paper. You lose!!");
           else if (computerPlay.equals("R"))
            System.out.println ("Paper eats rock. You win!!");
        }
        else if (personPlay.equals("S"))
        {
            if (computerPlay.equals("P"))
            System.out.println ("Scissor cuts paper. You win!!");
            else if (computerPlay.equals("R"))
            System.out.println ("Rock breaks scissors. You lose!!");
        }
        
        //Chooses Computer Choice
        
        public static String pickHandShape()
            {
                computerInt = generator.nextInt(3);
    
                switch (computerInt)
                {
                        case 0:
                                    {
                                    computerPlay = "R";
                                    break;
                                    }
                        case 1:
                                    {
                                    computerPlay = "P";
                                    break;
                                    }
                        case 2:
                                    {
                                    computerPlay = "S";
                                    break;
                                    }
                        default:
                                    {
                                    computerPlay = "will not happen";
                                    } 
                        
                }
            }
}

I'm not sure how to return the info in the class method.

I don't see what method you are talking about. Line 27 is in the main method which does NOT return any values (its void)

Please explain.

Do you get any errors when you compile the program?

Edited 6 Years Ago by NormR1: n/a

I don't see what method you are talking about. Line 27 is in the main method which does NOT return any values (its void)

Please explain.

I meant return whats in
public static String pickHandShape()

So what gets chosen in that gets put into line 27. Can that be done?

Loads of errors

RockPaperScissors.java:30: <identifier> expected
System.out.println("Computer plays: "+ computerPlay);
^
RockPaperScissors.java:30: illegal start of type
System.out.println("Computer plays: "+ computerPlay);
^
RockPaperScissors.java:30: ')' expected
System.out.println("Computer plays: "+ computerPlay);
^
RockPaperScissors.java:30: ';' expected
System.out.println("Computer plays: "+ computerPlay);
^
RockPaperScissors.java:30: illegal start of type
System.out.println("Computer plays: "+ computerPlay);
^
RockPaperScissors.java:30: <identifier> expected
System.out.println("Computer plays: "+ computerPlay);
^
RockPaperScissors.java:30: ';' expected
System.out.println("Computer plays: "+ computerPlay);
^
RockPaperScissors.java:32: illegal start of type
if (personPlay.equals(computerPlay))
^
RockPaperScissors.java:32: ';' expected
if (personPlay.equals(computerPlay))
^
RockPaperScissors.java:32: invalid method declaration; return type required
if (personPlay.equals(computerPlay))
^
RockPaperScissors.java:32: <identifier> expected
if (personPlay.equals(computerPlay))
^
RockPaperScissors.java:32: ';' expected
if (personPlay.equals(computerPlay))
^
RockPaperScissors.java:37: illegal start of type
else if (personPlay.equals("R"))
^
RockPaperScissors.java:37: ';' expected
else if (personPlay.equals("R"))
^
RockPaperScissors.java:37: illegal start of type
else if (personPlay.equals("R"))
^
RockPaperScissors.java:37: ';' expected
else if (personPlay.equals("R"))
^
RockPaperScissors.java:37: invalid method declaration; return type required
else if (personPlay.equals("R"))
^
RockPaperScissors.java:37: illegal start of type
else if (personPlay.equals("R"))
^
RockPaperScissors.java:37: ';' expected
else if (personPlay.equals("R"))
^
RockPaperScissors.java:44: illegal start of type
else if (personPlay.equals("P"))
^
RockPaperScissors.java:44: ';' expected
else if (personPlay.equals("P"))
^
RockPaperScissors.java:44: illegal start of type
else if (personPlay.equals("P"))
^
RockPaperScissors.java:44: ';' expected
else if (personPlay.equals("P"))
^
RockPaperScissors.java:44: invalid method declaration; return type required
else if (personPlay.equals("P"))
^
RockPaperScissors.java:44: illegal start of type
else if (personPlay.equals("P"))
^
RockPaperScissors.java:44: ';' expected
else if (personPlay.equals("P"))
^
RockPaperScissors.java:51: illegal start of type
else if (personPlay.equals("S"))
^
RockPaperScissors.java:51: ';' expected
else if (personPlay.equals("S"))
^
RockPaperScissors.java:51: illegal start of type
else if (personPlay.equals("S"))
^
RockPaperScissors.java:51: ';' expected
else if (personPlay.equals("S"))
^
RockPaperScissors.java:51: invalid method declaration; return type required
else if (personPlay.equals("S"))
^
RockPaperScissors.java:51: illegal start of type
else if (personPlay.equals("S"))
^
RockPaperScissors.java:51: ';' expected
else if (personPlay.equals("S"))
^
33 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.

You must have a missing { or } or an extra one somewhere that has confused the compiler.
Look thru your code to make sure there is a matching } for every {

Probably before line 30

Edited 6 Years Ago by NormR1: n/a

I'll look for it but is there way to return what happens in pickHandShape() to computerPlay?

Still getting those errors :3

I don't know if its a bracket issue, I can't see any missing or extra brackets.

//RockPaperScissors.java
//

import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors

{
    // Prompts user to choose: rock,paper, or scissors
    // Computer pseudo-randomly picks rock, paper, or scissors
    // Prints out who won, keeps going until user wins
    // Prints out how many times it took for user to beat computer
    
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        Random generator = new Random();
        String personPlay; //User's choice - R, P, or S
        String computerPlay; // Computer's choice - R, P, or S
        int computerInt; // Random generated number to choose computer's choice
        
        
        System.out.println("Enter R for Rock, P for Paper, S for Scissors: ");
        personPlay = scan.nextLine();
        personPlay = personPlay.toUpperCase();
        
        computerPlay = pickHandShape();
    }
    
    System.out.println("Computer plays: "+ computerPlay);
    
        if (personPlay.equals(computerPlay))
        { 
            System.out.println("It's a tie!");
        }
         
        else if (personPlay.equals("R"))
        {
            if (computerPlay.equals("S"))
            System.out.println("Rock crushes scissors.  You win!!");
           else if (computerPlay.equals("P"))
            System.out.println ("Paper eats rock. You lose!!");
        }
        else if (personPlay.equals("P"))
        {
           if (computerPlay.equals("S"))
            System.out.println ("Scissor cuts paper. You lose!!");
           else if (computerPlay.equals("R"))
            System.out.println ("Paper eats rock. You win!!");
        }
        else if (personPlay.equals("S"))
        {
            if (computerPlay.equals("P"))
            System.out.println ("Scissor cuts paper. You win!!");
            else if (computerPlay.equals("R"))
            System.out.println ("Rock breaks scissors. You lose!!");
        }
        
        //Chooses Computer Choice
        
        public static String pickHandShape()
            {
                computerInt = generator.nextInt(3);
    
                switch (computerInt)
                {
                        case 0:
                                    {
                                    computerPlay = "R";
                                    break;
                                    }
                        case 1:
                                    {
                                    computerPlay = "P";
                                    break;
                                    }
                        case 2:
                                    {
                                    computerPlay = "S";
                                    break;
                                    }
                        default:
                                    {
                                    computerPlay = "will not happen";
                                    } 
                        
                        return computerPlay;
                                    
                }
            }
}

I don't believe I do. Everything is basically happening in the main method except the random number generate for the computer in pickHandShape. I know it can all be done in one method but I need to use two (main and then main calls pickHandShape).

Really confused since nothing is working :/

Find the } at the end of each of the methods and look at what is after it.
There should NOT be any loose statement following that } that is outside of a method.

Edited 6 Years Ago by NormR1: n/a

Its been i while since i last did programming but ive noticed at line 28 that you've closed the main method.

try deleting the braket at line 28 and put it at the bottom of the code. It might expalin why every line after that point has an error

This question has already been answered. Start a new discussion instead.