Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default Methods - give hint

    i have a program that is basically like guessing a number between 1-20, and if the user gets it wrong, there should be a separate method which gives hint like try a higher number, try a lower, you won.

    it should look something along the lines of..
    enter a # between 1-20: 15
    hint: try a higher number

    enter a # between 1-20: 17
    hint: try a lower number

    enter a # between 1-20: 16
    you won!

    so far this is what i have
    Java Code:
    import java.util.Scanner;
    public class Ch7Ex10 {
       public static void giveHint() {
    
         
        if  (userNum < compNum) {
          System.out.println("Try a lower number");
        } else {
          System.out.println("Try a higher number");
        }
      }
      public void main (String[]args) {
        int  compNum = 16, userNum;
        Scanner input = new Scanner (System.in);
        
        System.out.println("Enter a number between 1 and 20: ");
        userNum = input.nextInt();
        
        if (userNum == compNum) {
          System.out.println("You won!");
        }else {
          giveHint();
        }
      }
    }
    help will be greatly appreciated!
    tips, solutions, anything will be good..
    by the way if you do give a solution, pls explain?

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Guess a number

    You really didn't ask a question or state a problem. In the future , try to specific exactly what the problem is and that way get better help.
    Back to your code... it won't compile. You have a scope problem. Variables "userNum" and "compNum" are defined in the "main" method, and are therefore not available in the "giveHint" method. There are two ways to get around this:
    • Pass these variables to the getHint method as arguments of the method (suggested way... best way)
    • Place these variables declarations in the class scope and that way they will be available to the getHint method.

    Helpful link:
    Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    yes thats my problem, it wont compile
    sorry for not specifying.
    anyways, so to pass the variables to the getHint method as args of the method, what do you mean by that??

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Ops.. misspell

    anyways, so to pass the variables to the getHint method as args of the method, what do you mean by that??
    Sorry about the misspell... I meant the "giveHint" method. Since I'm not sure your level of programming knowledge, I'll assume that you don't know what method arguments/parameters are.
    When you call a method, you can pass information to the method in the form of arguments/parameters. For example:
    Java Code:
    class myClass ()
     {
       public void main (String[]args) 
         {
          int [B][COLOR="Blue"]myFirstArg[/COLOR][/B] = 5;
          int [B][COLOR="blue"]mySecoundArg[/COLOR][/B] = 4;
          int answer = mySumMethod([B][COLOR="Blue"]myFirstArg, mySecondArg[/COLOR][/B]);
         }
    
    public static int mySumMethod([B][COLOR="blue"]int myArg1, int myArg2[/COLOR][/B]) 
         {
            int result = [B][COLOR="blue"]myArg1[/COLOR][/B] + [B][COLOR="blue"]myArg2[/COLOR][/B];
            return result;
         }
     }
    For more info, consult the following link:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    so how would you go about this in my case? :s
    i know you dont like giving away solutions, but i went on the links and read them and im still lost..
    sorry for the inconvenience :[

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    It's not so much about giving away the solution, it's about learning. It's obvious that even with the highlighted example that I posted, you have no clue want's going on. If I implement the solution for you, you will be exactly in the same situation as before and will have learned nothing.
    I'll try to explain further:
    • userNum and compNum are variables in your main program, ok?
    • You want to pass those variables to your giveHint method so you can compare them, right?
    • You can do that by passing these variable as arguments/parameters to the giveHint method (when you call the method):
      Java Code:
      public static void giveHint(compNum, userNum)
    • In the giveHint method declaration, you can declare the arguments with the same name or different name as the original variable name (I declared them different in my posted example).

    Did this help?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
     public static void giveHint(compNum, userNum) {
    
         
        if  (userNum < compNum) {
          System.out.println("Try a lower number");
        } else {
          System.out.println("Try a higher number");
        }
      }
      public void main (String[]args) {
        int  compNum = 16, userNum;
        Scanner input = new Scanner (System.in);
        
        System.out.println("Enter a number between 1 and 20: ");
        userNum = input.nextInt();
        
        if (userNum == compNum) {
          System.out.println("You won!");
        }else {
          giveHint();
        }
      }
    something like that? Yes, that does clear things up
    but I'm still wondering what it is I have to do to the code
    I mean, I know what's going on and what's happening

  8. #8
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    You're close, but still not quite there.

    In the method declaration for giveHint, you put the following:
    Java Code:
    public static void giveHint(compNum, userNum)
    That's close, but due to our obvious lack of experience, not quite right. Those two variables need to be created in the method declaration, thus:
    Java Code:
    public static void giveHint(int compNum, int userNum)
    Now those two variables can be used in the giveHint method, almost. First you have to pass the required variables to giveHint in the main method:
    Java Code:
    giveHint(firstVariable, secondVariable)
    I'll leave that to you to figure out, based on my hint above...

    Alternatively, and I think this is more simple in this case, you could declare the two variables (as CJLS suggested) in the class scope. This means that instead of
    Java Code:
    int compNum = 16, userNum;
    in the main method, you put it jsut beneath your class declaration:
    Java Code:
    class NumberGame {
        int compNum = 16, userNum;
        ...
    If you do this, you don't need to do any of the other stuff with the methods, and can leave the rest of your code as it was. I still recommend that you read up about methods, as it is quite crucial to start learning some Java...

  9. #9
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    public class Ch7Ex10 {
    public void main (String[]args) {
        int  compNum = 16;
        userNum;
        Scanner input = new Scanner (System.in);
        
        System.out.println("Enter a number between 1 and 20: ");
        userNum = input.nextInt();
        
        if (userNum == compNum) {
          System.out.println("You won!");
        }else {
          giveHint();
        }
      }
    public static void giveHint(int compNum, int userNum) {
    
         
        if  (userNum < compNum) {
          System.out.println("Try a lower number");
        } else {
          System.out.println("Try a higher number");
        }
      }
     
    }

  10. #10
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    I take it you neglected to read my post in it's entirety?
    Last edited by carderne; 01-04-2009 at 01:07 PM.

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default oh boy...

    iWonder... before we go on, I need you to answer a question:
    • Do have any Java or programming experience/knowledge at all?

    Because if the answer is no, then we cannot go any further with this method stuff. We can not help you to walk if you can't crawl. It's obvious that you don't have a clue of what is going on. Two people have tried to show you how and have not had success.
    I suggest that you go through the basics of Java programming and work on it until you come to the method declarations. Here's a good link to start:

    The Java™ Tutorials

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    I do have knowledge, and we have learned methods in class.
    I just dont understand it the way my teacher explains it to me.

    And carderne: I did read your post

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default One last time....

    I'll try to be as clear as possible...
    In your code you have the following:
    Java Code:
    giveHint();
    This code is calling the giveHint method. It is missing something (hint: missing exactly two things):
    • What two things does your giveHint method need so it will work?
    • What two things (hint: variables?) do you need to compare?


    If after this, you still don't know how to pass variables to a method, you still have a couple of options left:
    • have your teacher explain it to you or...
    • Using your original code, place the variable declarations:

    Java Code:
    int  compNum = 16, userNum;
    within the class scope, which would be under the class statement:
    Java Code:
    public class Ch7Ex10 {
    [COLOR="Blue"][B]int  compNum = 16, userNum;[/B][/COLOR]
    public void main (String[]args) {...
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  14. #14
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    public class Ch7Ex10 {
    int compNum = 16, userNum;
    public void main (String[]args) {
    
        Scanner input = new Scanner (System.in);
        
        System.out.println("Enter a number between 1 and 20: ");
        userNum = input.nextInt();
        
        if (userNum == compNum) {
          System.out.println("You won!");
        }else {
          giveHint();
        }
      }
    public static void giveHint(int compNum, int userNum) {
    
         
        if  (userNum < compNum) {
          System.out.println("Try a lower number");
        } else {
          System.out.println("Try a higher number");
        }
      }
    }

  15. #15
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Since your not passing the variables to the method, your method can't have arguments. Change this:
    Java Code:
    public static void giveHint(int compNum, int userNum)
    to this:
    Java Code:
    public static void giveHint()
    and your set.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  16. #16
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    thank you so much.
    I appreciate you putting up with my lack of knowledge with methods.

    Sorry for my inconvenience.. but thanks again!

  17. #17
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Your welcome, but I do strongly suggest you get a handle on methods. If your going to be programming (in any language), it's something that is used day in and day out.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  18. #18
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    Congratulations. Yes, what you need to understand is that your method call:
    Java Code:
    giveHint();
    must correspond to your method declaration:
    Java Code:
    public static void giveHint()
    So if your method declaration has two variables in it:
    Java Code:
    public static void giveHint(int var1, int var2)
    Then your method call must also have two variables, of the same type:
    Java Code:
    giveHint(myNum, otherNum);
    Where myNum and otherNum are obviously of type int.

    Good luck.

  19. #19
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Thanks, carderne
    i did it this morning without seeing your post and i got an error and i was wondering if your suggestion would fix this error since i dont have dr.java at home...
    http://i39.tinypic.com/2e4vtwy.gif the grey highlight is the error.. its usually yellow but i dont know why it turned grey :/

    if it doesnt, suggestions please?

  20. #20
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    iWonder, do you know what static means?
    inside static method, no non-static global variable can be used

    and your program fix the value of compNum which is not really a guessing game
    Last edited by mtyoung; 01-06-2009 at 05:24 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. how to give delay?
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-17-2008, 06:57 PM
  3. Please give me a solution
    By ivvgangadhar in forum AWT / Swing
    Replies: 5
    Last Post: 11-14-2008, 02:43 PM
  4. code hint scroll bar ?
    By tryit in forum Eclipse
    Replies: 3
    Last Post: 04-28-2008, 08:07 AM
  5. give me a advise
    By sireesha in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 02:38 AM

Posting Permissions

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