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.

