Re: help needed on inputting - tearing hair out .. please!!!



Patrick:

Thanks for posting all the code for this application. I will
definitely evaluate by tonight.

In the meantime, you may be able to get something out of the example I
found on my CD. It was a small application that Chuck Allison
published in one of his articles a few years ago. See
http://www.freshsources.com/ for more about Chuck Allison's work. The
code is as follows:

import java.io.*;

public class HiLo
{
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
boolean done = false;
while(!done)
{
boolean found = false;
int lo = 1;
int hi = 100;
int guess = 0;
while(!found && lo <= hi)
{
guess = (lo + hi) / 2;
System.out.println("Is it " + guess + "?");
char r = in.readLine().toUpperCase().charAt(0);
if(r == 'H')
lo = guess + 1;
else if(r == 'L')
hi = guess - 1;
else if(r != 'Y')
System.out.println("Try again...");
else
found = true;
}
if(lo > hi)
System.out.println("You cheated!");
else
System.out.println("Your number was " + guess);
System.out.println("Want to play again?");
done = in.readLine().toUpperCase().charAt(0) != 'Y';
}
}
}

Please contact me if you have any questions...

Mike.

.



Relevant Pages