Skip to content

Instantly share code, notes, and snippets.

@yukeehan
Created January 23, 2018 21:21
Show Gist options
  • Save yukeehan/aba339f4f49b2b66a03305eaa32cacaf to your computer and use it in GitHub Desktop.
Save yukeehan/aba339f4f49b2b66a03305eaa32cacaf to your computer and use it in GitHub Desktop.
do-while loop to discard any unwanted character remaining in the input buffer
package chapter3;
public class TryThis3_2 {
public static void main(String args[])
throws java.io.IOException{
char choice,ignore;
do {
System.out.println("1.if 2.switch 3.for 4.while 5.do-while\nchooose one");
choice = (char)System.in.read();
do {
ignore = (char)System.in.read();
}while(ignore != '\n');
}while(choice<'1'|choice>'5');
System.out.println("\n");
switch(choice) {
case'1': System.out.println("if blablabal"); break;
case'2': System.out.println("switch blabal"); break;
case'3': System.out.println("for balbal"); break;
case'4': System.out.println("while balbal"); break;
case'5': System.out.println("do-while blabla"); break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment