Trying to understand how the guess game works in Java -
public class guess { public static void main(string arg[]) throws java.io.ioexception { char ch, ignore, answer = 'k'; { system.out.println("i'm thinking of letter between a-z, can guess it?"); ch = (char) system.in.read(); { // <<question block, start** ignore = (char) system.in.read(); } while (ignore != '\n'); // <<<end block in question here** if (ch == answer) system.out.println("***winner***"); else { system.out.print("...sorry "); if (ch > answer) system.out.println("toooo lowwww "); else { system.out.println("tooooo highhh "); } } } while (answer != ch); } } how line make program execute properly? understand takes on value not "\n" if delete whole piece of code program executes every single if, else statement. how allowing appropriate line execute?
this first post here, please let me know if have done wrong or against rules.
thank you.
do { ignore = (char) system.in.read(); } while (ignore != '\n'); this code continue read characters standard input until reads newline. discard additional characters until game prompts user character again.
you have entered abc first time. without code, if got answer wrong, game prompt letter, read b entered first time.
Comments
Post a Comment