• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HeadFirst Java 2nd Edition

 
Bob Cook
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just starting out learning Java using this text book. I finished reviewing chapter 2 and have run the GuessGame in Eclipse as written in the book. I am trying to make a change so that within the player object I can store the number of times a player guesses a specific number (5 for example). I then want to have this printout at the end of the game to say "player 1 guessed 5 x times), player 2 guessed 5 x times, etc....

I made the following changes to the code and thought I am getting no errors this is not working as intended so I am definitely missing something here. Below is the code in the player and guess game classes with my changes. Any suggestions to point me in the right direction would be appreciated.








 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made the following changes to the code and thought I am getting no errors this is not working as intended so I am definitely missing something here.


It would probably help if you tell us what you consider "working as intended" or what do you consider "missing something".

But here's a hint...



Where is the p1guessFive variable set? And what is it set to?

Henry
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

You've defined a counter in your Player class as an int variable called "guessFive." That looks fine.

But in your GuessGame class, you define String variables for each player (like "p1guessFive") and set these to null. These have no relation to the int counters in your Player instances, nor do they ever get assigned a value other than null. Do you see why?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49365
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
I don't have that book (I ought to buy a copy) but I have seen that guessing game many times: here on the beginner's forum. A search for "GuessGame" will probably find several previous discussions, and you can see how others handled their problems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic