Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random numbers

 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20893
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The biggest issue is that you're nesting incorrectly. You want something like:

 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic