Results 1 to 4 of 4
Thread: i need help with this code
- 05-28-2010, 01:47 AM #1
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
i need help with this code
my teacher gave us this assignment: make a program that lets the user guess a random number and compare it against one generated by the program, if they win, say so, else if they lose tell them they lost.
I came up with this :
i get a lot of errors when it try to compile it and it doesnt work.Java Code:import java.scanner; public class rand { public class int guess { public class Scanner = new system.in(); public class randNum; randNum = public class Math.random() * public 20 + private 1; system.in.println("enter a guess);" +; guess = public class scanner.nextInt(); else if(randNum = public class guess) system.in.print("you won); + "; else { system.in.println("you lost)" + ; } public main void static(String args) { run(l); }} }
- 05-28-2010, 02:07 AM #2
When posting questions about errors, please copy and paste here full text of the error messages.i get a lot of errors when it try to compile it
You're using the public keyword in many wrong places. Its mainly used when defining a class, method or class variable. Not for local (in a method) variables.
The class keyword is only used when you define a class. In your program you are only defining one class: rand
Note: The convention for class names is to start with uppercase
- 05-28-2010, 02:09 AM #3
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
My advice: throw out this code and start over as it's not worth your while fixing this as it's completely and utterly borked (sorry for the bluntness).
Before starting again, reread your book and your notes because you have to have an idea of what you're writing before trying to commit code to screen. Check out sample programs from your class and use them as templates for your next attempt.
When you restart writing this program you should only add one line at a time and compile each time you add a line. If there are any compile errors, then read the error messages carefully -- they'll tell you exactly what is wrong, and use these error messages to guide you in fixing the errors before adding one more line of code. If you do it this way, you'll avoid having a mess of garbled code.
Best of luck, and come on back with your next attempt.
- 05-28-2010, 02:15 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Thank you for using CODE tags and making a good effort to format your code. Having said that (and I'm really not trying to be mean) have you been paying attention in class at all?
Where did you get this from? You'll make very slow progress if you just make wild guesses about which packages to import. Check your text and your examples again.Java Code:import java.scanner;
This is legal, but not very good style. Choose a noun for your class name and start it with an upper-case letter. GuessNumberProgram is a much better class name than rand.Java Code:public class rand {
A class does not return an int. You probably want to define a method here, and this is not the correct syntax to do so. Look at your text and examples again.Java Code:public class int guess {
This is not how you initialize a Scanner object. Not even close. It's not cheating to look at a reference while you're learning, and it will be a very slow and painful process if you don't.Java Code:public class Scanner = new system.in();
Another class? With no class body? This makes no sense at all. You only want one class for this program, and so far you seem to be trying to declare four.Java Code:public class randNum;
OK, you must be trolling, and here I am feeding the troll. I'll post what I've written so far for the amusement of lurkers, but this is as far as I go.Java Code:randNum = public class Math.random() * public 20 + private 1;
-Gary-
Similar Threads
-
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03-19-2008, 10:12 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks