really simple question about SWITCH and CASE cmmnds in JAVA

Discussion in 'Mac Programming' started by neelvaka, Sep 18, 2007.

  1. neelvaka macrumors newbie

    Joined:
    Sep 15, 2007
    #1
    Code:
                do {
                                                            System.out.print("\tMy guess is " + (hi+lo)/2 + ". Is that (l)ow, (h)igh, or (c)orrect? ");
                                                            input = IN.readLine();                                                       
                                                            if ((hi+lo)/2 == hi - 1 || (hi+lo)/2 == 1) {  
                                                                    System.out.println("\tYou are a cheater! You fail at life.");
                                                                    break; }
                                                            input = IN.readLine();
                                                            count++;          
                                                            switch(input.charAt(0)){
                                                                    case 'l':
                                                                      	lo = (hi + lo) / 2;                                                                    
                                                                            break;
                                                                    case 'h':
                                                                    	hi = (hi + lo) / 2;
                                                                            break;
                                                                    case 'c':
                                                                      	System.out.println("\n\tI guessed your number in only " + count + " tries!");
                                                                        System.out.println("\nThank you for playing Guessing Game.");
                                                                        	break;                                       
    } 
                    } while ( ! (input.equals("c") || input.equals("C")) );
    for some reason when a question is asked to the user and he/she submits either l,h, or c it need to be entered TWICE before the program does soemthing and goes to asking another questions

    IE:
    is your number 5? (low high or correct): and then i put in "l"
    and then i have to put in "l" again
    then it says
    is your number 7? (low high or correct):

    what is wrong with this?
    it seems like a simple mistake
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    Probably because you are performing two readlines. I saw it as soon as I unindented your code.
     
  3. neelvaka thread starter macrumors newbie

    Joined:
    Sep 15, 2007
    #3
    yeah haha i caught that too srry stupid mistake of me

    ty though :)
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    Since the user can exit the loop by typing 'c' or 'C', I think you should handle 'L', 'H' and 'C' in the switch statement as well.
     
  5. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #5
    Which is why it pays to format your code properly.
     

Share This Page