brainfrz

Untitled

Sep 26th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. // Hmm, why is the first one fine, but he second one causing an infinite loop when failing?
  2.  
  3. // First one
  4. String number;
  5. int highestFactor;
  6. do
  7. {
  8.     System.out.println("Please enter how high you'd like to multiply (up to 20):  ");
  9.     number = user_input.next();
  10.  
  11.     try {
  12.         highestFactor = Integer.parseInt(number);
  13.     } catch (NumberFormatException e) {
  14.         System.out.print("That's not a number! ");
  15.         highestFactor = -1;
  16.     }
  17. } while (highestFactor <= 0 || highestFactor > MAX_FACTOR);
  18.  
  19. // Second one
  20. int highestFactor;
  21. do
  22. {
  23.     try {
  24.         System.out.println("Please enter how high you'd like to multiply (up to 20):  ");
  25.         highestFactor = user_input.nextInt();
  26.     } catch (InputMismatchException e) {
  27.         System.out.print("That's not a number! ");
  28.         highestFactor = -1;
  29.     }
  30. } while (highestFactor <= 0 || highestFactor > MAX_FACTOR);
Advertisement
Add Comment
Please, Sign In to add comment