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
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!
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.