# Methods - give hint

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-02-2009, 06:33 AM
iWonder
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
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?
• 01-02-2009, 03:49 PM
CJSLMAN
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.

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

Luck,
CJSL
• 01-02-2009, 09:01 PM
iWonder
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??
• 01-02-2009, 09:36 PM
CJSLMAN
Ops.. misspell
Quote:

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:
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;     }  }```
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Luck,
CJSL
• 01-02-2009, 09:46 PM
iWonder
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 :[
• 01-02-2009, 10:13 PM
CJSLMAN
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):
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
• 01-03-2009, 05:04 AM
iWonder
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
• 01-03-2009, 01:39 PM
carderne
You're close, but still not quite there.

In the method declaration for giveHint, you put the following:
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:
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:
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
Code:

`int compNum = 16, userNum;`
in the main method, you put it jsut beneath your class declaration:
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...
• 01-04-2009, 05:52 AM
iWonder
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");     }   }   }```
• 01-04-2009, 12:55 PM
carderne
I take it you neglected to read my post in it's entirety?
• 01-04-2009, 04:03 PM
CJSLMAN
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
• 01-04-2009, 09:32 PM
iWonder
I do have knowledge, and we have learned methods in class.
I just dont understand it the way my teacher explains it to me.

• 01-04-2009, 10:25 PM
CJSLMAN
One last time....
I'll try to be as clear as possible...
In your code you have the following:
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:

Code:

`int  compNum = 16, userNum;`
within the class scope, which would be under the class statement:
Code:

```public class Ch7Ex10 { [COLOR="Blue"][B]int  compNum = 16, userNum;[/B][/COLOR] public void main (String[]args) {...```
Luck,
CJSL
• 01-05-2009, 01:05 AM
iWonder
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");     }   } }```
• 01-05-2009, 01:14 AM
CJSLMAN
Since your not passing the variables to the method, your method can't have arguments. Change this:
Code:

`public static void giveHint(int compNum, int userNum)`
to this:
Code:

`public static void giveHint()`

CJSL
• 01-05-2009, 01:36 AM
iWonder
thank you so much.
I appreciate you putting up with my lack of knowledge with methods.

Sorry for my inconvenience.. but thanks again!
• 01-05-2009, 01:44 AM
CJSLMAN
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
• 01-05-2009, 05:20 PM
carderne
Congratulations. Yes, what you need to understand is that your method call:
Code:

`giveHint();`
must correspond to your method declaration:
Code:

`public static void giveHint()`
So if your method declaration has two variables in it:
Code:

`public static void giveHint(int var1, int var2)`
Then your method call must also have two variables, of the same type:
Code:

`giveHint(myNum, otherNum);`
Where myNum and otherNum are obviously of type int.

Good luck.
• 01-05-2009, 11:39 PM
iWonder
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 :/