Hi Lo

<< | APQuestionsTrailIndex | >>

HiLoGameRunner.java

import java.util.Scanner;
public class HiLoGameRunner
{
    private  Scanner IO;   

    public static void main(String[] args)
    {
        Scanner IO = new Scanner(System.in);
       // String a=IO.nextLine();
        HiLoGame game = new HiLoGame(IO);
        game.giveInstructions();
        game.play();
    }
}

HiLoGame.java

import java.util.Scanner;
public class HiLoGame
{
    private static Scanner IO;
    private int computerGuess;
    public HiLoGame(Scanner IO){
        computerGuess = 0;
        this.IO=IO;
    }
    public void giveInstructions(){
       System.out.println("Think of a whole number between 1 and 100");
    }  
   public  void play(){
       int counter =0;
       int high=100;
       int low = 1;
       String response="h";
       while (response.equals("h") || response.equals("l") ){
       computerGuess=(high+low)/2;
       // Output computer's guess
        System.out.println ("Is it a "+computerGuess+"? (type \"h\" for higher,\"l\" for lower, anything else if it is correct");
        //  Ask if it is correct
         response = IO.nextLine();

        if (response.equals("h"))
            low=computerGuess;
        if (response.equals("l"))
            high=computerGuess;   
        counter++;
       }
       // Print the number of guesses 
       System.out.println("It took "+counter+" guesses");
    }

}