Results 1 to 13 of 13
- 10-16-2008, 08:39 AM #1
Need help with Rock Paper Scissors Game
Hey I'm new to the forums and to Java. I have been writing a rock, paper, scissors game and almost everything seems to be in order. But I have a line of code that contains an error I cannot catch. The compiler says "illegal start of expression when the game ends and prints the users statistics. Here's the code:
// Author:
// Started: October 14, 2008
// Finished:
//
// Game of Rock, Paper, Scissors
//
import jpb.*;
public class RPS
{
public static void main(String[ ] args)
{
// Instance variables
int wins = 0;
int losses = 0;
int draws = 0;
boolean repeat = true;
// Loop so the program can run as many times
// as the user would like
while(repeat = true)
{
SimpleIO.prompt("Rock, paper, or scissors?");
String choice = SimpleIO.readline( );
if(choice.equalsIgnoreCase("rock"))
{
System.out.println("You lose");
losses ++;
}
else if(choice.equalsIgnoreCase("paper"))
{
System.out.print("It's a draw");
draws ++;
}
else if(choice.equalsIgnoreCase("scissors"))
{
System.out.println("You win!");
wins ++;
}
else
{
System.out.println("I'm sorry, that's not an answer");
SimpleIO.prompt("Rock, paper, or scissors?");
String choice = SimpleIO.readline( );
}
// Asks the user if they want to continue
SimpleIO.prompt("Play again?");
String decision = SimpleIO.readline( );
if(decision.equalsIgnoreCase("yes"))
repeat =true;
else if (decision.equalsIgnoreCase("no"))
{
repeat = false;
// Prints the user's stats
System.out.print(wins + " wins " + losses + " losses " + );
System.out.print(draws + " draws");
}
// Displays a message depending on performance
if (wins > losses)
{
System.out.println("Good job, thanks for playing.");
}
else
{
System.out.println("It wasn't your best day.");
}
else if
{
SimpleIO.prompt("That is not an option. Play again?");
String decision = SimpleIO.readline( );
}
}
}
}
}
The error is in the two lines that print the user's stats in the middle of the program. This may seem elementary, but I am very new to Java. Thanks in advance.
- 10-16-2008, 09:35 AM #2
on line 51: change thisJava Code:C:\jexp>javac rps.java rps.java:51: illegal start of expression System.out.print(wins + " wins " + losses + " losses " + ); ^ rps.java:64: 'else' without 'if' else if ^ rps.java:71: class, interface, or enum expected } ^ 3 errors
to thisJava Code:System.out.print(wins + " wins " + losses + " losses " + );
Java Code:System.out.print(wins + " wins " + losses + " losses ");
- 10-16-2008, 10:35 AM #3
thanks
thanks that pretty much fixed it. It's amazing how you can overlook such things.
- 10-17-2008, 02:52 AM #4
reached end of file while parsing
the program is almost error free. but the IDE I am running and even DOS highlights my last curly brace and says 'reached end of file while parsing'. I tried adding and taking away curly braces and have gotten varied but unwanted results. Taking one away gives me more errors in dealing with my input statements. Adding one also gives me errors with my input statements. Any suggestions or explanations?
- 10-17-2008, 04:13 AM #5
Some IDEs have logic to find the matching brace. Check yours.
Otherwise, print out the source and take a pencil and connect the pairs of braces until you find the missing one. Work from the inside out.
- 10-17-2008, 06:46 AM #6
confusing
Wow when I added the matching braces (it's equal now) there are four errors in the program each dealing with my input statements. On every line that contains "string x = SimpleIO.readline()". The error message is "cannot find symbol" (new line) "symbol : method readline()" (new line)
"location: class jpb.SimpleIO"
Is the compiler telling me that SimpleIO isn't in the package I used? What about the readline() method? It wasn't saying that before I evened the curly braces.
- 10-17-2008, 09:13 AM #7
As SimpleIO isn't a class in the standard JDK, you need to ask wherever you got that class from.
About mismatching braces: learn to indent your code correctly and it'll never happen again. Well, almost never.
Code Conventions for the Java(TM) Programming Language: Contents
luck, db
- 10-17-2008, 02:33 PM #8
IF the missing class (SimpleIO) is in a jar file, you need to add that jar file to the classpath when you compile and execute your program so that the javac and java programs can find it.
- 10-17-2008, 03:59 PM #9
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
You have this
and a bracket mismatch somewhere.Java Code:else { System.out.println("It wasn't your best day."); } else if {
And if you're sure it is where it should be, it probably is readLine(), with a capital LI die a little on the inside...
Every time I get shot.
- 10-19-2008, 04:29 PM #10
My code was indented, but when i pasted it, it wasn't shown that way. Yes capitalizing the readLine worked, but now it is saying that my string variable that is the user's decision, is already defined in main. I don't understand how I can get 1 error after another. It's like Java is never satisfied. Getting kind of frustrating.
Last edited by GettinGwap; 10-19-2008 at 04:39 PM.
- 10-19-2008, 04:39 PM #11
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
Use the CODE or PHP tags to get indentation.
- 10-19-2008, 05:31 PM #12
You need to post the FULL text of the error messages so we can see what and where the errors are.
for example:is already defined
int aInt = 123;
...
int aInt = 45; // here aInt is already defined above
- 10-19-2008, 06:15 PM #13
I finally got it to work. Thank you for sticking with me the enitre time. That was my first program that I attempted on my own outside of schoolwork. Again thanks. Now I want to put it on my phone. It is an LG Fusic. I googled it but I found most people were concerned with ring tones.
Last edited by GettinGwap; 10-19-2008 at 06:28 PM.
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
game
By amith in forum AWT / SwingReplies: 0Last Post: 05-19-2008, 05:16 PM -
Paper,Scissor,Rock If then Statements
By Alberto in forum New To JavaReplies: 2Last Post: 02-11-2008, 11:18 PM -
urgent help needed - paper submission.
By dirtycash in forum New To JavaReplies: 2Last Post: 11-23-2007, 11:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks