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