@rolfl @rolfl and @Mat's Mug @Mat's Mug have provided valuable insights on your question about comments, so I'll attempt to tackle the code part of it...
edit @rolfl's @rolfl's approach is better and I'll recommend it over the above.
@rolfl and @Mat's Mug have provided valuable insights on your question about comments, so I'll attempt to tackle the code part of it...
edit @rolfl's approach is better and I'll recommend it over the above.
@rolfl and @Mat's Mug have provided valuable insights on your question about comments, so I'll attempt to tackle the code part of it...
edit @rolfl's approach is better and I'll recommend it over the above.
nextInt()
throws a InputMismatchException
if a non-integer input is entered, and not only that you must remember to 'consume' the current line by doing a nextLine()
at the end. I'll suggest reading the entire line in, do a Integer.parseInt(line)
and catching the NumberFormatException
for invalid inputs. You can then repeatedly prompt the user until you get a valid integer (maybe you'll want to exclude negative values, or numbers outside your specified guessing range).
private static void printEither(boolean condition, String format,
String ifTrue, String ifFalse) {
System.out.printf(format + "%n", condition ? ifTrue : ifFalse);
}
nextInt()
throws a InputMismatchException
if a non-integer input is entered, and not only that you must remember to 'consume' the current line by doing a nextLine()
at the end. I'll suggest reading the entire line in, do a Integer.parseInt(line)
and catching the NumberFormatException
for invalid inputs. You can then repeatedly prompt the user until you get a valid integer (maybe you'll want to exclude negative values, or numbers outside your specified guessing range.
private static void printEither(boolean condition, String format,
String ifTrue, String ifFalse) {
System.out.printf(format + "%n", condition ? ifTrue : ifFalse);
}
nextInt()
throws a InputMismatchException
if a non-integer input is entered, and not only that you must remember to 'consume' the current line by doing a nextLine()
at the end. I'll suggest reading the entire line in, do a Integer.parseInt(line)
and catching the NumberFormatException
for invalid inputs. You can then repeatedly prompt the user until you get a valid integer (maybe you'll want to exclude negative values, or numbers outside your specified guessing range).
private static void printEither(boolean condition, String format,
String ifTrue, String ifFalse) {
System.out.printf(format + "%n", condition ? ifTrue : ifFalse);
}