Hello all,
I'm a student who just began a course in Java programming and I'm trying to create a program which generates a random number between 0-99 and then asks the user to guess the number. If the guess was close to the correct answer, the program informs the user. I have tried to get the code to compile and run for several hours now (using NetBeans), and I keep getting various error messages like the one in the title of this thread. Here is the code:
/*This is a number guessing game*/ package labfirst; import java.util.Random; import java.util.Scanner; public class Guess { public static void main(String[] args) { //Here the program should create a random number generator Random random = new Random(); //The program asks the user to input a number and generates a random number Scanner scanner = new Scanner(System.in); System.out.print("Guess a number between 0-99: "); int ranInt = random.nextInt(100); int userInt = -1; } while (userInt!=ranInt) { System.out.print("Incorrect! Guess again: "); userInt = scanner.nextInt(); } /*The program compares the user's input with the randomly generated number and comments on whether the number is correct, incorrect or close*/ if (userInt==ranInt - 5) { System.out.print("The number I was thinking of was " + ranInt + " , you were close."); } else if (userInt==ranInt + 5) { System.out.print("The number I was thinking of was ") + ranInt + (" , you were close."); } else if (userInt==ranInt) { System.out.print("Congratulations, the number was ") + ranInt + (" , you win!"); System.exit(0); } }
Please forgive my ignorance, I'm really new at this. I've edited and re-edited the code so many times (searching online for help) that I'm sure there are more errors now than there were to begin with . This is the exact error message:
Exception in thread "main" java.lang.ClassFormatError: Duplicate field name&signature in class file labfirst/Guess
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java :792)
at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader .java:449)
at java.net.URLClassLoader.access$100(URLClassLoader. java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:4 24)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 57)
at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:482)
Java Result: 1