Welcome to Dream.In.Code
Become a Java Expert!

Join 150,381 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,219 people online right now. Registration is fast and FREE... Join Now!




Need help fixing a problem

 
Reply to this topicStart new topic

Need help fixing a problem, Rock paper scissor program

kyeong
18 Feb, 2008 - 12:45 PM
Post #1

New D.I.C Head
*

Joined: 12 Feb, 2008
Posts: 22

I'm having a bit of trouble trying to solve this problem. I looked at it and did some stuff for an hour with no success. Here's the code:

CODE

{
    public static void main(String[] args)
    {
    String personPlay;    //User's play -- "R", "P", or "S"
        String computerPlay = "";  //Computer's play -- "R", "P", or "S"
    int computerInt;      //Randomly generated number used to determine
                          //computer's play

    Scanner scan = new Scanner(System.in);
    Random generator = new Random();


    //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";
            System.out.println ("The computer has chosen rock. ");
            break;

        case '1':
            computerPlay = "P";
            System.out.println ("The computer has chosen paper. ");
            break;

        case '2':
            computerPlay = "S";
            System.out.println ("The computer has chosen scissors. ");
            break;

        default:
            System.out.println("Please re-enter a value between 0-2. ");

    }






    }
}


The error I get is "variable computerPlay might not have been initialized."

Thanks.


I had fixed the problem, thanks to dontKnowJava, but now I'm having another problem. When I execute the program, the computer doesn't generate one of the 3 choices by itself. It just says "Please re-enter the values betweens 0-2."

Thanks.

This post has been edited by kyeong: 19 Feb, 2008 - 10:19 AM
User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: Need Help Fixing A Problem
18 Feb, 2008 - 12:47 PM
Post #2

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
String computerPlay = "";

This post has been edited by dontKnowJava: 18 Feb, 2008 - 12:48 PM
User is offlineProfile CardPM
+Quote Post

kyeong
RE: Need Help Fixing A Problem
18 Feb, 2008 - 12:52 PM
Post #3

New D.I.C Head
*

Joined: 12 Feb, 2008
Posts: 22

That doesn't seem to work or maybe I'm putting it in the wrong place.

I get this error "computerPlay is already defined in main(java.lang.String[])".

Thanks.

EDIT: Never mind, I got it. Thanks.

This post has been edited by kyeong: 18 Feb, 2008 - 01:04 PM
User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: Need Help Fixing A Problem
18 Feb, 2008 - 01:43 PM
Post #4

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
your switch statement is looking for chars not ints get rid of quotes around numbers but then you still have logic error computer tells you his choice before you pick yours
User is offlineProfile CardPM
+Quote Post

kyeong
RE: Need Help Fixing A Problem
19 Feb, 2008 - 10:17 AM
Post #5

New D.I.C Head
*

Joined: 12 Feb, 2008
Posts: 22

Thanks. All I had to do was remove the quotes next to the case numbers. The int still worked and I can't change it because my professor already had that in there.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:13PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month