-7

I am beginner in java. Please provide a sample program in Java to play a number-guessing game. The game works as follows:

The user chooses a number in his mind and types “ready” to indicate to the computer that he is ready to

begin playing.

The computer asks a series of questions to arrive at the number the user has in mind. The user can only

respond with “higher”, “lower” or “yes”.

The game ends when the user responds with “yes” or “end”.

Example:

User chooses number 40 in his mind.

Computer: Is the number 30?

User: higher

Computer: Is the number 50?

User: lower

Computer: Is the number 35?

User: higher

Computer: Is the number 40?

User: yes

  • 1
    Have you tried writing some java code for it? First try to spend a couple of hours writing the program and show if to us when you have problems, then we will help you. We will and should not do the work for you. – ruthless Sep 05 '14 at 04:11
  • 1
    Welcome to StackOverflow; I believe you misunderstood the purpose of this website. You should provide a specific development problem that you encounter, not ask for people to do your job. Start by explaining what you have tried, what you get, and what you do not understand. – personne3000 Sep 05 '14 at 04:12
  • Good question, try for your goodness first and back if any problem.!! – Wundwin Born Sep 05 '14 at 04:14

2 Answers2

4

Firstly : You shouldn't ask for sample code without any of your own code - that's likely why this is being downvoted.

Moving on, you should decide how you want your program to guess. For example, a bisection algorithm might be useful for you here. You'd need to set some initial code which differed, in order to bound the person's number.

Eg: (pseudo-code)

int guessLower=0;
int guessUpper=1000000;
int myGuess=10;

// Ask user to pick number
// Ask user if number is == guess
// If yes, finish.
// If the answer is higher than the guess, change guessLower to the value of guess, and change guess to halfway between guess and guessUpper.
// If the answer is lower, do the opposite (guessUpper = myGuess; myGuess = (guessLower + myGuess)/2;

// Repeat until finished

This (~)code won't work if the value the person makes up isn't within the limits you set - so consider asking first if it is higher than guessLower, and lower than guessUpper. If not, try moving the markers by some amount. More complicated algorithms for searching can be easily imagined, and you should be careful about the differences between integers and doubles etc.

On a side note, for comparing strings, you should look up using equals(...), eg input.equals("higher");. Don't use the C++ == for comparing the values of strings, I anticipate this might be a problem you'll hit...

chrisb2244
  • 2,940
  • 22
  • 44
  • Here is the code i written. But it is not fully functional as per my requirement. – Kirk logan Sep 05 '14 at 12:16
  • Where? I don't see any code, or really any example of code - just output that you're looking for. Try forming some code, or at least a code structure. I suspect if you can form something like what I've written in this answer, and add some more detail, you'll be pretty close to what your end code looks like. From there, you'll just need to find implementation details. The first answer to the linked question (http://stackoverflow.com/questions/17538182/getting-keyboard-input) looks like it might be helpful for your input needs. – chrisb2244 Sep 05 '14 at 14:57
1

I won't write or provide a program for you, this is pretty basic.

A few bits to get you started:

You need to use a Scanner object to read in from the keyboard. This question should cover that part pretty well: Getting Keyboard Input

You also need to understand booleans. Click here for that.

And finally you should probably learn about "relational operators," like > is greater than, < is less than, etc. Check here for that.

Community
  • 1
  • 1
Daniel B.
  • 1,254
  • 1
  • 16
  • 35