High and Low Game using limited resource

Hi Masters!,

My assignment is to think of a number (in head) between 1-100 where program will eliminate options by h or l. I've checked online and most of the code is by using

import java.math.*;

import java.util.Random;

import java.util.Scanner;

I don't have the luxury but just the Scanner with IF + while loop. This is what I have so far don't know where to go now with it? Any hints?

Code:

`public static void main(String [] args)`

{

Scanner keyboard = new Scanner (System.in);

int highestValue = 100;

int lowestValue = 1;

int guesses = 0;

int number;

char C,H,L;

boolean high, low, correct;

String pcGuess;

high = true;

low = true;

correct = true;

System.out.print("Is the number 50? ");

pcGuess = keyboard.nextLine;

if(pcGuess == correct);

System.out.print("It took me 1 guess!");

}

}

Re: High and Low Game using limited resource

output should be something like this:

----jGRASP exec: java Assignment6b

Is the number 50? H

Is the number 75? L

Is the number 62? L

Is the number 56? L

Is the number 53? L

Is the number 51? C

It took me 6 guesses!

----jGRASP: operation complete.

Re: High and Low Game using limited resource

Just think about it. If the number is between 1 and 100, and the computer guesses 50, and you tell the computer that the number is higher, what is the number between now?

Think about how you'd play this game with a friend. Write out the steps you'd follow. When you're done with that, you'll have an algorithm that should be pretty easy to translate into code.

Re: High and Low Game using limited resource

Quote:

Originally Posted by

**KevinWorkman** Just think about it. If the number is between 1 and 100, and the computer guesses 50, and you tell the computer that the number is higher, what is the number between now?

Think about how you'd play this game with a friend. Write out the steps you'd follow. When you're done with that, you'll have an algorithm that should be pretty easy to translate into code.

I just don't know where I'm going with this =(, can't think of right algorithm....

Code:

`boolean number, h, l, c;`

String pcGuess;

h = true;

l = true;

c = true;

System.out.print("Is the number 50? ");

pcGuess = keyboard.nextLine();

if(pcGuess = h || pcGuess !=l || pcGuess !=c)

Re: High and Low Game using limited resource

Don't worry about the code or even a computer for a minute. Pretend you're playing with a friend, and you have to guess the number he's thinking. How do you do it? Write out your thought process on a piece of paper. Understanding how to solve problems, especially problems where the solution feels like "I don't know, I just do it" is a crucial skill to programming, but it's one that's almost impossible to teach. That's one of the real points of the assignment.

Re: High and Low Game using limited resource

Write out the pseudo-code for your program first so you can get organized and design your plans or the program.

Your going to need a loop that continuously asks "Is the number x" , where x is the variable of one of the guesses.

You can logically eliminate the other values and slowly refine the guesses by using the Higher/lower indication until you have the exact number.

Re: High and Low Game using limited resource

Quote:

Originally Posted by

**KevinWorkman** Don't worry about the code or even a computer for a minute. Pretend you're playing with a friend, and you have to guess the number he's thinking. How do you do it? Write out your thought process on a piece of paper. Understanding how to solve problems, especially problems where the solution feels like "I don't know, I just do it" is a crucial skill to programming, but it's one that's almost impossible to teach. That's one of the real points of the assignment.

Ok,

I ask my friend to pick a number between 1-7 (in head)

1 2 3 4 5 6 7

I will ask him is that number 4?

friend answers L // that tells me that all the numbers from 4-7 are not his number

i will ask him again is that number 2?

friend answers L// that tells me that 2-3 is not his number so I know he picked number "1"

So I guess your number is 1 // he says yes

I know the reasoning behind it but I cant/don't know how to put in into algorithm and loop so it works like on a piece of paper ......its so easy and so hard at the same time. I'm going to kill me self in just a minute ......

Re: High and Low Game using limited resource

Well, you know you'll need a loop that iterates as long as a correct guess was not made. How do you do that?

Then you have some handy variables ready to store the lowest and highest values it can be, based on the answers you've received so far.

Re: High and Low Game using limited resource

Quote:

Originally Posted by

**KevinWorkman** Well, you know you'll need a loop that iterates as long as a correct guess was not made. How do you do that?

Then you have some handy variables ready to store the lowest and highest values it can be, based on the answers you've received so far.

am i going the right way with the loop?

Code:

`boolean number, h, l, c;`

String pcGuess;

h = true;

l = true;

c = true;

System.out.print("Is the number 50? ");

pcGuess = keyboard.nextLine();

while(pcGuess == h)

{

if(pcGuess != h && pcGuess == l && pcGuess == c)

{

System.out.print("is the number 25? ");

pcGuess = keyboard.nextLine();

}

Re: High and Low Game using limited resource

small update - still working

while(h == true)

{

System.out.print("is the number 75? ");

pcGuess = keyboard.nextLine();

if

Re: High and Low Game using limited resource

I'm not sure what h is supposed to represent. You want to loop while the guess is not equal to the answer. For that, you're going to need ints that store the last guess and the answer. I'm not sure what all those booleans are for.

Re: High and Low Game using limited resource

Kevin,

first of all thank you for trying to help me with this exercise I really appreciate your "time" BUT think i need to go over loops and if statements again because i'ts not working.... here is the code just fyi:

Code:

`public static void main(String [] args)`

{

Scanner keyboard = new Scanner (System.in);

int highestValue = 100;

int lowestValue = 1;

int guesses = 1;

int count = 0;

int userNumber;

//int number;

//char C,H,L;

boolean number, h, l, c;

String pcGuess;

h = true;

l = true;

c = true;

System.out.print("Is the number 50? ");

pcGuess = keyboard.nextLine();

while(h == true)

{

userValue = highestValue-51;

System.out.print("is the number 75? ");

pcGuess = keyboard.nextLine();

if(h == true || h != c)

{

System.out.print("is the number 87? ");

}

Re: High and Low Game using limited resource

I think you're over-complicating things with all those extra booleans. What do they represent? By that, I mean, how do they translate to your description of how you'd do this with a friend? I understand the ints representing your changing idea of what the number could be, so you might want to focus on them instead.

And if you want to go over loops again, the tutorials are your first stop: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)