Guessing Game

import javax.swing.JOptionPane;

import java.util.Scanner;

publicclass Game{

public Game(){

int count = 0;

int high = 128;

int low = 1;

int guess = (high+low)/2;

JOptionPane.showMessageDialog(null,"Think of a number between " + low" and " + high);

while(true){

count = count + 1;

int input = JOptionPane.showConfirmDialog(null,"Is " + guess +" your number?");

if (input==JOptionPane.CANCEL_OPTION)break;

if (input==JOptionPane.YES_OPTION){

JOptionPane.showMessageDialog(null,"Congratualations the number is"+" "+guess+" "+"and it took me"+" "+count+" "+"tries");

break;

}

if(input==JOptionPane.NO_OPTION){

String input2 = JOptionPane.showInputDialog("Is the number higher or lower?");

if (input2==null)break;

Scanner scan =new Scanner(input2);

String answer = scan.next();

if(answer.equalsIgnoreCase("higher")){

guess = (guess + high)/2;

}

if(answer.equalsIgnoreCase("lower")){

guess = (guess + low)/2;

}

}

}

}

publicstaticvoid main(String[] args){

new AssignmentThree();

}

}

Basically I need to find a way to make it work... for example lets say I pick the number 33. First it'd ask me if it was higher or lower than 64, so I'd say lower, so it'd ask me if it was higher or lower than 32, so I'd say higher. But the current code wouldn't set a range to ask me between 32-64, instead it'd ask me if my number was higher or lower than 80. I'd need it to ask me if the number was higher or lower than 48 and so on.

I had documentation to help me with this on my laptop but apparently it wasn't saved so I'm stuck having to figure it out myself and I haven't come up with any ideas yet so help me out.

I already know that I need to adjust the high and low after the higher and lower if statements, but I don't know how to execute this in one statement without ruining the other statement like below:

if(answer.equalsIgnoreCase("higher")){

guess = (guess + high)/2;

low = guess - 1;

}

if(answer.equalsIgnoreCase("lower")){

guess = (guess + low)/2;

high = guess + 1;

}

This code, when I use my test number, 22, it just loops on asking me if my number is 17.

I understand whats going on but I don't know how to fix it. And I'll take care of the formatting and if's and else if's as soon as I can get the whole thing working.

[4408 byte] By [HeartCokea] at [2007-11-26 19:10:51]
# 1
Why the need to create yet another seperate topic on this? Now where would you like your answers, here or there? Both? I don't think so. http://forum.java.sun.com/thread.jspa?threadID=5139412
warnerjaa at 2007-7-9 21:07:19 > top of Java-index,Java Essentials,Java Programming...
# 2
Answered in your other thread http://forum.java.sun.com/thread.jspa?threadID=5139412Ted.P.s. nice one on the new subject ;o)
ted_trippina at 2007-7-9 21:07:20 > top of Java-index,Java Essentials,Java Programming...
# 3
Wanerja, he only doing it cus he got told to take the "URGENT" out the subject. He's learnt his lesson (unlike the other trogladites on this forum).Ted.
ted_trippina at 2007-7-9 21:07:20 > top of Java-index,Java Essentials,Java Programming...
# 4
No need for this ultra slow garbage, move along!Message was edited by: SomeoneElse
SomeoneElsea at 2007-7-9 21:07:20 > top of Java-index,Java Essentials,Java Programming...