# java quessing game

• November 4th, 2013, 02:04 PM
brox
java quessing game
Hello,
i have some troubles with my homework... i need to make quessing game...
i think random number 1-100 and program will queesing this number and asking me for question and i will reply only yes/no
my idea is make lots of this:
System.out.println("is your number greater than 50? yes/no");
String a = sc.next();
if(a.equals("yes")){
System.out.print("is your number geater than 85? yes/no")
.
.
.
.
}
but it is soooo long.. can you help with some better solution?
thank you very much and sorry for my low english :))
• November 4th, 2013, 02:33 PM
Norm
Re: java quessing game
Use variables vs hardcoding numbers:
Code :

`"is your number greater than 50? yes/no");`
Replace the 50 with a variable that starts at 50 and then is computed as the game goes on.
Something like this:
first = 1
last = 50
top = 100
when > 50, last = (top - last)/2

That's a very rough idea. It needs some work.
• November 4th, 2013, 02:46 PM
brox
Re: java quessing game
wow.
It is still hard for me..
i dont understand the principle of this.. please can you write me something.. more?
thank you
• November 4th, 2013, 02:52 PM
Norm
Re: java quessing game
Try doing the program in a series of small steps:
First step:
Change your current code to use variables as I suggested. Ask the question using the variable.
Change the variables to new values depending on the user's response. Print out the new values of the variables and the first step is done.

A way to visualize the problem would be two write a line with numbers from 1 to 20 (not 100 for now). Set a ptr to the number the user has chosen and pointers to the beginning and end of the range of possible value.
Now do a binary search by asking the user relative to the middle of the range.
If above the middle, move the lower range marker up to the middle and compute a new middle. If user says below move the top marker to the middle. As you ask questions one or the other marker will move. Eventually there will be one number between/at the markers.
• November 4th, 2013, 03:06 PM
brox
Re: java quessing game
so.. i made 3 variables first,last and top
so.. now i will replace last(50) with 25 ?
edit:
now i have this
int first = 1;
int last = 50;
int top = 100;
System.out.print("is you number lesser than " +last+ "?");
String a = sc.next();
if(a.equals("yes")){
last = 25;
}
• November 4th, 2013, 03:15 PM
Norm
Re: java quessing game

If the response is not less that means greater. Change the bottom/left to the middle.
Have you tried the visual exercise I suggested?
• November 4th, 2013, 03:27 PM
brox
Re: java quessing game
i tried to imagine it like a axis
|----|----|
the middle "|" is 50.
if userīs answer is "is not greater than 50" the axis will be
|--|-----|
after this, program will ask "its your number greater than 25?" userīs answer will be ... no. so axis will be
|-|------|
sorry if i understand wrong..
• November 4th, 2013, 03:30 PM
Norm
Re: java quessing game
That's probably right. Now try writing the simple one step program I suggested:
set initial values
move markers as per user's response
print out new value of markers
• November 4th, 2013, 03:49 PM
brox
Re: java quessing game
yes i have it.
i will try to do it for right side of axis.

--- Update ---

oh god... its really hard for me... its so many lines... i have only this and i am lost in it..
System.out.print("is your number lesser than " + last + "?");
String a = sc.next();
if (a.equals("yes")) {
last = 25;
}
System.out.print("is your number lesser than " + last + "?");
String b = sc.next();
if (b.equals("yes")) {
last = 12;
} else if (b.equals("no")) {
last = 38;
}
System.out.print("is your number lesser than " + last + "?");
String c = sc.next();
if (c.equals("yes")) {
last = 32;
} else if (b.equals("no")) {
last = 45;
}
• November 4th, 2013, 03:56 PM
Norm
Re: java quessing game
That is NOT the code I was talking about. Try again. Write code that only does the simple first step as discussed in posts #8 and #4. Make a complete class, that will compile and execute. Do it: One small step at a time.

Be sure to wrap your code with code tags:
[code=java]
[/code]
to get highlighting and preserve formatting.
• November 4th, 2013, 04:17 PM
brox
Re: java quessing game
i still thinking about it... so .. if userīs answer is "lesser" the only thing i change is "last(50)" wil be "last(25)" right ?
i working in Eclipse so i think i dont need that tags :)
my code:
Code java:

```package project;   import java.util.Scanner;   public class Game { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int first = 1; int last = 50; int top = 100; System.out.print("is your number lesser than " + last + "?"); String a = sc.next(); if (a.equals("yes")) { last = 25; } System.out.print("is your number lesser than " + last + "?"); String b = sc.next(); if (b.equals("yes")) { last = 12; } System.out.print("je vaše číslo měnší než " + last + "?"); String c = sc.next(); if (c.equals("ano")) { last = 6; } System.out.print("je vaše číslo měnší než " + last + "?"); String d = sc.next(); if (d.equals("ano")) { last = 3; } System.out.print("is your number lesser than " + last + "?"); String e = sc.next(); if (e.equals("yes")) { last = 2; } } }```
• November 4th, 2013, 04:18 PM
Norm
Re: java quessing game
[code=java]
[/code]
to get highlighting and preserve formatting.

The code should set the values of the variables with hardcoded values ONLY at the start.
After that the variables values are copied from other variables or computed.

The only two initial values would be 1 and 100. The middle would be computed. That would allow the range to be changed to 1 to 20 for easier testing.
• November 4th, 2013, 04:21 PM
brox
Re: java quessing game
oh sorry I didnt know that you mean post
• November 4th, 2013, 04:28 PM
Norm
Re: java quessing game
Copy the statement(s) that you don't understand and ask a question about it.

In this statement:
last = 25;
25 is a hardcoded value. last's value should be computed based on the previous values of first, top and last. Look at the row of number I asked you to use to see how the markers are to be moved.

What happens to the logic of that statement if the range of numbers is changed to 1 - 20 for testing?
• November 4th, 2013, 04:38 PM
brox
Re: java quessing game
please can you write me code for range 1 - 20 ? because i can do it same as i wrote for range 1-100 but it will be just
Code java:

```package project;   import java.util.Scanner;   public class Game { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int first = 1; int last = 10; int top = 20; System.out.print("is your number lesser than " + last + "?"); String a = sc.next(); if (a.equals("yes")) { last=last/2; } System.out.print("is your number lesser than " + last + "?"); String b = sc.next(); if (b.equals("yes")) { last = last/2; }   } }```

i really dont know how to solve it i am beginner..
• November 4th, 2013, 04:42 PM
Norm
Re: java quessing game
I suggested that you work on the problem, one small step at a time. You seem to have ignored my suggestion and are trying to write the whole program at once.

Try writing the code for the small first step as I suggested.

If you'd rather write all the code at once, I'll leave and let some one else try to help you.
• November 4th, 2013, 04:57 PM
brox
Re: java quessing game
I know how to imagine it. But I dont have idea how to write it. I know..there is values top and first but I dont know how to use them in code ? How to compute middle value?
• November 4th, 2013, 05:05 PM
Norm
Re: java quessing game
The code you have posted shows some of the statements you will need. Trying to write the whole program at once will make for confusion. Doing it one step at a time will be a better approach.
Are you going to write the program I suggested in posts #4 and #8?
• November 4th, 2013, 05:06 PM
brox
Re: java quessing game
I dont know how to write it..
• November 4th, 2013, 05:10 PM
Norm
Re: java quessing game
Code java:

```package project;   import java.util.Scanner;   public class Game { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int first = 1; int last = 10; int top = 20; System.out.print("is your number lesser than " + last + "?"); String a = sc.next(); if (a.equals("yes")) { last=last/2; } System.out.print("is your number lesser than " + last + "?"); String b = sc.next(); if (b.equals("yes")) { last = last/2; }   } }```

Quote:

I dont know how to write it..
Who wrote the code you posted in post#15? That has most of what the small first step should have.