PDA

View Full Version : really simple question about SWITCH and CASE cmmnds in JAVA




neelvaka
Sep 18, 2007, 10:52 PM
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



toddburch
Sep 18, 2007, 11:32 PM
Probably because you are performing two readlines. I saw it as soon as I unindented your code.

neelvaka
Sep 19, 2007, 12:15 AM
Probably because you are performing two readlines. I saw it as soon as I unindented your code.

yeah haha i caught that too srry stupid mistake of me

ty though :)

gnasher729
Sep 19, 2007, 03:21 AM
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.

savar
Sep 19, 2007, 01:05 PM
yeah haha i caught that too srry stupid mistake of me

ty though :)

Which is why it pays to format your code properly.