aspose file tools*
The moose likes Beginning Java and the fly likes Random numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random numbers" Watch "Random numbers" New topic
Author

Random numbers

Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
I need to ask the user for a numer between 1 and 1000. The computer should pick a random number. It needs to say if the user's guess is higher or lower than the random number. Here is my code:

//Stephanie Dears
//8 November 2005
//CMIS 241

import java.util.Random;
import java.io.*;

public class GuessNumber {

public static void main(String[] args) {

public int guess{ //I get an "illegal start of expression" error

int myNumber;
myNumber= 0;
int number;

Random rng = new Random();
number = 1 + rng.nextInt(1000);

int number = scan.nextInt();

System.out.print("Please enter a number between 1 and 1000");
Scanner stdin = new Scanner(System.in);

if (number < mynumber) {
System.out.print("The number you guessed is high. ");
}
else if (number > mynumber) {
System.out.print("The number you guessed is low");
}

else {
System.out.print("you are right");
}

if (number> mynumber || number < mynumber) {
System.out.print("thanks , bye");
}
} //end method numberGuess -- I get a "; expected" error
} //end method main

} //end class
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40



It is an error because it is not valid java code inside of a method... actually, this is not valid java code outside of a method either.

What are you trying to do?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
The biggest issue is that you're nesting incorrectly. You want something like:



<a href="http://forums.hotjoe.com/forums/list.page" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
Scott,

Thanks. I have always had this problem in Java. I'm not sure where to put things. As far as I can tell, nothing belongs within the main {}.

Can you give me a clue?

Also, is the code, not necessarily the order of the code, seem right?

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Stephanie Dears:
...is the code, not necessarily the order of the code, seem right?

The concept behind this code is basically correct, once you separate the methods as Scott suggested.

The key idea for you at this point is that methods cannot be defined within other methods. You have a problem because "guess" is defined within "main."

The main method is your "entry point" into the program. In this case, you will probably want to use main to create a new instance of GuessNumber, and call the method guess().

(Also note that Scanner is in java.util, so you will need another import statement. And Java is case sensitive, so mynumber is not the same as myNumber.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
If you're having trouble making sure braces match (& who doesn't?), consider downloading & using a good IDE that will alert you to mismatches. Personally, I like (httpEclipse. It will handle indentation automatically so brace problems are even easier to spot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random numbers