
-----------------------------------
cool dude
Sun May 07, 2006 12:22 pm

guessing game
-----------------------------------
this is my first sort of game i made thanks to wtd tutorials and help :) 
any suggestions on improving code would be appreciated


import java.io.*;

public class GuessNum1 {
    public static void main(String[]args) {
        
        //Declare all variables here
        String input2;      //stores the input as a string
        int input;          //stores the converted input as an integer
        int floor = 0;      //stores the lowest bound that the number can be
        int ceiling = 100;  //stores the highest bound that the number can be
        String ans = "no";  //stores if the user made up a number yet
        int guess;          //stores the computers guess 
        String hint2;       //stores the hint higher/lower/correct as string
        int hint;           //stores the hint higher/lower/correct as integer
        
        try{
            BufferedReader c = new BufferedReader(new InputStreamReader(System.in));
            
            do{
                System.out.println("Make a number up in your head between 1 and 100");
                System.out.println("Have you made a number up? yes/no");
                ans = c.readLine();
            }while (ans.equals("no"));
        
            System.out.println("The computer will try to guess your number");
            System.out.println("if the computer guessed lower please enter '1'.\nif the computer guessed higher please enter 2.\nif the computer guessed correct please enter 3");
            System.out.println("");
            
            do{
                guess = (floor + ceiling) / 2;
                System.out.println("The computer guesses " + guess);
                hint2 = c.readLine();
                
                hint = Integer.parseInt(hint2);
                
                switch(hint){
                    case 1:
                        floor = guess;
                        break;
                    case 2:
                        ceiling = guess;
                        break;
                    case 3:
                        System.out.println("The computer guesses correct! ");
                        break;
                    }
                }
                while(hint != 3);
                        
        }
          catch (NumberFormatException nfe) { 
             System.out.println("can't convert: "); 
          } 
          catch (IOException f) { 
             System.out.println("error reading input"); 
          }        
    }
}

-----------------------------------
xHoly-Divinity
Wed May 10, 2006 5:08 pm


-----------------------------------
You should make it so that the program automatically identifies if the computer is >//