• Post Reply Bookmark Topic Watch Topic
  • New Topic

The Guessing Game - Java  RSS feed

 
Noel O'Kane
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently doing an assignment for my class called the guessing game. The game entails a player starting up a session and entering a number between 1-100 and they are limited to 6 attempts and when they are out of attempts or guess the number they are prompted with a dialogue box asking if they want to play again. If yes the game repeats, if no heir attempts are input to a high score text file that can be viewed after every session. I have gotten as far as the player being asked if they want to play again or not but I am completely lost on how to start a new session and what to do with adding the code for the high score text file. Any indication of where to go from here or what to do would be greatly appreciated, I have posted the original source code below. Thanks.
 
Noel O'Kane
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I tried to edit you post to add code tags which usually make it look better, but something went wrong. I might try again a bit later and see if I can get it right

Why are you putting everything in the main method?
You start off with an incorrect assumption, about numbers up to 100. That is what everybody thinks about Random, but it ain't right.
Why are you using JOptionPane? That is very old‑fashioned programming style.
Never write true && It is simply a waste of keystrokes. The compiler might remove true && from the output, but I don't know about that.
What are you doing with the input from where you request a name?
You have lots of repeated code where you are showing dialogues with almost the same message. That sort of code should be moved out into a method of its own.
You ask how you should repeat the whole game if the user enters yes. Do you know you can collect that input as an int and use it later? You seem to be using a == on that input, but not using the result you get from the ==.
Are you aware of the sort of loop which runs once and then tests at the end whether it should be repeated? You can find all the kinds of loop in the Java Tutorials.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I was mistaken. You are using that value in an if. Don't use System.exit() which might not do any harm here, but it is a bit vicious and can cause serious harm in a threaded application by stopping all threads suddenly. You should stop the application by finishing the loop, and then it will finsh all the methods and stop all systems gracefully.
 
Noel O'Kane
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your quick reply, I am using JOptionPane as I only need a simple user interface which can be easily understood by the user, I am in my first year of Programming and am so far only using simpler console interfaces. I would just like to get the whole program working first as described in my specification before putting in the loop and taking out the System.exit, it closes my program fine at the moment though. My problem is the coding for the high score text file which I need to write in the session for the user, when they complete a game it is saved as one attempt and if they choose again it wil be a second attempt, and the one with the best score ie whichever turn guessed the number first is put down as their best attempt of N/A if they did not guess it.

I have checked through my lecture notes and personal notes and cannot seem to get my head around what needs to be coded to get this to work.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case you have a major piece of equipment not working as required. It is black (usually) long and thin and connects the computer to the wall. It should be removed. If the screen goes completely blank, then the operation has been successful.
Then you move up the Periodic Table. You note computers run on silicon and above Si in the periodic table you find C for carbon. Carbon is what pencils are made of, and a pencil is what you want, and maybe some paper. Also an eraser, which will be very important.
You write down what you want to happen, and you then get it into words of one syllable and then you can consider turning your computer on again. You should also tell us how the game is collecting scores and how you intend to write them to the file.
Actually your code is very non‑object‑oriented. You don't have any Game objects, nor Player objects. You hardly seem to have any objects at all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!