Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java guessing game

 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a lot of trouble coding a java guessing game. Its from 1-100 and shows how many guesses it took to guess it right. Once you guess it right, it asks if you want to repeat. if the ans you write starts with a "y", it repeats; otherwise it prints the total games and total tries(i haven't gotten to stats yet) . Here is what i have so far...


 
Winston Gutkowski
Bartender
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P Derlyuk wrote:Here is what i have so far...

OK, so what happens when you run it? Presumably, not what you expect, so it would be useful if you could TellTheDetails (←click).

However, my general advice in cases like this is always the same: StopCoding.

Go back and write down the steps needed to play the game, in English, and see how they match up with the code you've written. You'll probably find that you can answer most of your own questions.

Just one other general observation: Your code is quite tightly coupled.
Have you considered a class that encapsulates a single run of the game? Don't worry about it too much until you get your code working, but you may find that it makes your program a lot tidier.

Winston
 
Daryle Foulke
Greenhorn
Posts: 8
IntelliJ IDE Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's not working as you expect? I'm not going to run your code, but I will help if you have a questions other then heres my code, whats wrong?
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im having trouble defining the parameters for the method that plays one game. That method is then repeated if the user wants to with a do-while loop. Im not sure if it should only take a scanner and/or a rand.
 
Daryle Foulke
Greenhorn
Posts: 8
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tired compiling you program? I don't think it will even compile. I would do that first and run what you have. Are you getting the results you want?
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiled and ran when all the code was all in main. My instructor then specified that she wanted to play one game in a method and then repeated in main with do-while so that is what i attempted to do. I attached the code before i tried to put it in a method and repeat it. That's where i believe i screwed it up.





 
Daryle Foulke
Greenhorn
Posts: 8
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a little hint to get you going. Try to get it to compile as you have it first. i think that would be the best place to start. Why are you calling your playOneGame function twice? also when calling a function you dont need to define the variable types.




maybe



Give that a try and see what else you need to change. Then just play around with it until you get the results your looking for.
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! That was my method problem. I'm working on the program so it prints the stats when the user is finished playing. Ill write back if any other problems arise.
Thank you so much for your help so far.
 
P Derlyuk
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I kinda restructured the program but it works with your tips!
Thanks a bunch Daryle!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic