| Author |
If else loop error
|
Brian Walsh
Greenhorn
Joined: Feb 06, 2002
Posts: 17
|
|
I have been having problems in comiling a program and I was wondering how do I get rid of this error: E:\School\CS161\Workbench\HiLow.java:37: 'else' without 'if' else Here is my code( it is not completely done yet...) import cs1.Keyboard; public class HiLow { //----------------------------------------------------------------- // Plays a simple guessing game with the user. //----------------------------------------------------------------- public static void main (String[] args) { String str, another = "y"; int GUESSNUM, MAX = 100; float RANNUM = 0; float TRY = 0; System.out.print ("I'm thinking of a number between 1 and " + MAX + ". Guess what it is: "); GUESSNUM = Keyboard.readInt(); while (another.equalsIgnoreCase("y")) // allows y or Y { if (RANNUM = 0) RANNUM = (int) (Math.random() * MAX) + 1; else do if ( GUESSNUM > RANNUM) System.out.print ("You are guessing high, try again? (y/n)"); another = Keyboard.readString(); else if ( GUESSNUM < RANNUM) System.out.print ("You are guessing low, try again? (y/n)"); another = Keyboard.readString(); else System.out.print ("Your on the Money way to go!!!"); System.out.print ("You wanna play again? (y/n)"); another = Keyboard.readString(); } } } Thanks
|
-Thanks in Advance
|
 |
Kevin Magee
Greenhorn
Joined: Feb 02, 2002
Posts: 18
|
|
Hi, I think this might help you. if your "if loop" specifies more than one action you need to enclose the conditions in brackets. Same with the else. In fact, I think all Java loops follow this rule!! hope this helps Kevin [ February 06, 2002: Message edited by: Kevin Magee ] [ February 06, 2002: Message edited by: Kevin Magee ]
|
 |
Brian Walsh
Greenhorn
Joined: Feb 06, 2002
Posts: 17
|
|
I only have one condition in these loops...and that is if (the guessed number is larger then the random number) then it will kick into the if statement.. Thanks I don't know why it isn't seeing the if statement wihtout the else.
|
 |
Brian Walsh
Greenhorn
Joined: Feb 06, 2002
Posts: 17
|
|
here is the code to make it easier to look at then the original post.
|
 |
Kevin Magee
Greenhorn
Joined: Feb 02, 2002
Posts: 18
|
|
Sorry I've messed up my explanation. the code you've post is missing brackets. I can't seem to add code properly. The problem your having is after each if (condition) you need a bracket. This bracket tells the complier, that everything from that point till the next bracket is part of the if. By default, the complier assumes that only one action will be performed. In your code you there are no brackets, but two actions to be perform if the if condition is met. The complier only sees one action, and when it comes to the else statement, it can't associate that with the if because you have a rogue statement sitting between them. Just put a bracket a the end of your if line and before the else. e.g. if (a > b){ a += 1; b = c;} else{ a =c; b +=1;} that should work Kevin [ February 06, 2002: Message edited by: Kevin Magee ]
|
 |
Brian Walsh
Greenhorn
Joined: Feb 06, 2002
Posts: 17
|
|
|
THANK YOU...IT DID
|
 |
 |
|
|
subject: If else loop error
|
|
|