Results 1 to 4 of 4
  1. #1
    ineedHelp2 is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default 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 :

    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);
        }}
    }
    i get a lot of errors when it try to compile it and it doesnt work.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    i get a lot of errors when it try to compile it
    When posting questions about errors, please copy and paste here full text of the error messages.

    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

  3. #3
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    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?
    Java Code:
    import java.scanner;
    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:
    public class rand {
    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 int guess {
    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 Scanner = new system.in();
    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 randNum;
    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:
            
            randNum = public class Math.random() * public 20 + private 1;
    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.

    -Gary-

Similar Threads

  1. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  2. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •