[Logo]
Forums Register Login
Unable to Return Variable
This program's supposed your typical random number generator. It doesn't seem to be passing the int Number back from the NumbertoGuess method for use in the rest of the program. What am I doing wrong here? I'm aware there are some missing things in this program, I'm concerned about getting the value returned properly right now.

Nothing looks wrong with NumbertoGuess. It is returning the number.

The question is - What makes you think that its not returning a number?
Thanks for the help, Sam.

I've been doing some debugging with Ststem.out.println and trying to track the variables and their values as the program runs. If you execute the code, it will let you pick a number but will always tell you the number is lower than your guess. When you guess either a 0 or guess twice, it says you've guessed correctly and asks if you'd like to play again.

Far as I can tell, it's not passing the randomized Number variable back into the public void main for use in the next method in the series once it's generated in NumbertoGuess. I see the number generated correctly within NumbertoGuess but always see a value of 0 with a println at line 15 or 16 when I check it.
Java is pass by value, so passing Number as a parameter to the method will not cause Number to be changed.
What you should have is


Note that I've changed the name of the variable in NumbertoGuess to make it obvious that it is not the same variable as the Number instance variable
Alright, that makes sense. So how do I get the value of num out of the method for use in the rest of the program? Does the returning of num pass that value as the int num or back to NumbertoGuess?
Your have several problems with this code...

1 You don't adhere to Sun's naming recommendations.

Sun Naming Conventions

2. Read this article to understand pass-by-value and pass-by-reference for Java.

Java parameter passing

The third issue is a result of trying to manipulate the parameter instead of capturing the return value from your method. NumbertoGuess returns a value- and you aren't storing it into anything!

Ooppss, sry Joanne! This rotten connection is so sloooow (didn't see your post) I might as well have mailed in my post-
That's what I get for going from a pass-by-reference language like VB to Java. lol

I'll mess around with it for a bit and see what I come up with. Thanks for the help!
 
Jared Osborne wrote:Alright, that makes sense. So how do I get the value of num out of the method for use in the rest of the program?


Thats what the line 'return num;' does.

Jared Osborne wrote:Does the returning of num pass that value as the int num or back to NumbertoGuess?


Note how I also changed your call to the method. The returned value will be assigned to Number
Workin' like a charm now. Thanks for the help, all.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Dec 15, 2017 20:40:31.