• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help understanding example code: HeadFirst Java 2nd Ed. (GuessGame)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, this has been posted a few times before, and I'm seeing some answers, but I'm really looking to understand *why* this doesn't work.

I'm pretty sure I busted this code out a year or so ago and it worked fine. Now I'm having all sorts of interesting dilemmas (or probably one interesting dilemma manifesting itself a few times).

Three Players should randomly guess a randomly generated target numbers. Rounds continue to be played til at least one gets it right.

I'm posting the code with my comments, which include what I'm trying to understand and what I think might be happening.






[ May 24, 2008: Message edited by: Gavin Ralston ]
 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





You have stumbled upon an issue of scope.

Inside the method, you've declared a completely different variable, also called number. This is the variable that you are giving the "guess" value, and when your method has finished and goes out of existence, so does this value. All you have left to access is your instance variable, which was never given a value at all, and which happens to default to zero.

So your game should be over pretty quickly if the number to guess is 0, since all the players get it right the first time!

There are a couple of things you could experiment with:

1) inside your method, instead of saying "int number", try using "this.number"
this refers to "the object to which I belong". Since each player has their own guess() method, then that method would know to look for a variable inside that particular instance of the class.
2) remove the type declaration inside your method. That is to say, use

instead of

[ May 24, 2008: Message edited by: Katrina Owen ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Yep, that's shadowing: there's a local variable inside the method which records the guess; it disappears immediately after the method is called, and the "number" member variable is never set to any value.

The fix is incredibly simple. Your method says

int number = (int) (Math.random() * 10);

which is basically shorthand for

int number; // Declare a new variable named "number"
number = (int) (Math.random() * 10); // Give the local "number" a value

instead you just want to write

number = (int) (Math.random() * 10); // Give the member "number" a value

Removing those three little character "int" at the beginning of the line makes all the difference!
 
Gavin Ralston
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to both of you for catching that! It worked fine.

It didn't matter how many times I looked at the guess() method, I wasn't going to see I was declaring a totally new variable there. Heck, I even heavily commented the section and *still* couldn't see the insidious "int"

I think my confusion regarding the "if guessp1 == true" tests was more to do with me continually recompiling the Player.java file. I may have never actually compiled a version where I assigned a value to 'number' and thus didn't have a chance of lucking out of the loop sing targetNumber would never equal null.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic