This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi, I am not sure what is wrong with the following code, and was hoping that someone would shed some light on why I am getting the following errors when I try to compile. newGame.java:55: cannot resolve symbol symbol : variable number location: class newGame else if (test > number) ^ newGame.java:63: cannot resolve symbol symbol : variable number location: class newGame else if (test < number) ^ 2 errors
----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. PLEASE HELP!! (here is the code) import javax.swing.*; import java.text.*; import java.util.Random; public class newGame { public static void main(String[] args) { Random generator = new Random(); int number = generator.nextInt(1000); String userInput = JOptionPane.showInputDialog("Give me a number between 0 and 1000"); int stringLength = userInput.length(); int guess = Integer.parseInt(userInput); int checkedGuess = errorCheck(guess); } public static int errorCheck(int test) { if (test > 1000) { return 0; } else if (test > number) { return 1; } else if (test < number) { return 2; } else return 3; } }
Because you aren't passing number into the method errorCheck. Brush up on the Java Language Basics, in particular, scope [ October 03, 2003: Message edited by: Joe Ess ]
Adding to what Joe said, A variable declared in one method is known only to that method. Thus, declaring number in main() does not make it known to errorCheck(). In other words, the scope of a variable declared in a method is limited to that method.
For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
Code tags don't help if you don't format your code. [ October 03, 2003: Message edited by: Marilyn de Queiroz ]
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
jason candelora
Greenhorn
Joined: Sep 25, 2003
Posts: 16
posted
0
I am not sure what you mean..I just pasted the code in from JGrasp and tried to make it more readable for you... I also figured out that I needed to declare my variable outside of the main method to use them in more than that one method, it seems to work ok now... thanks for the help Jason