Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

@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.

added 83 characters in body
Source Link
h.j.k.
  • 19.3k
  • 3
  • 37
  • 93

edit@rolfl's approach is better and I'll recommend it over the above.

edit@rolfl's approach is better and I'll recommend it over the above.

added 8 characters in body
Source Link
h.j.k.
  • 19.3k
  • 3
  • 37
  • 93

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);
}
added 136 characters in body
Source Link
h.j.k.
  • 19.3k
  • 3
  • 37
  • 93
Loading
Source Link
h.j.k.
  • 19.3k
  • 3
  • 37
  • 93
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /