diff --git a/src/Factorials.java b/src/Factorials.java index 05ca73e102a32e0d260d22bd97cec9be3cebe6fa..85d7f0f8bd45c69a4f84f4fd87c4256b5f4b8223 100644 --- a/src/Factorials.java +++ b/src/Factorials.java @@ -50,18 +50,21 @@ public class Factorials { String keepGoing = "y"; Scanner scan = new Scanner(System.in); - while (keepGoing.equalsIgnoreCase("y") ) - { - System.out.print("Enter an integer: "); - int val = scan.nextInt(); - if (val<=-1){ - - throw new IllegalArgumentException("Valore minore di -1"); + while (keepGoing.equalsIgnoreCase("y") ) { + int val = 0; + try { + System.out.print("Enter an integer: "); + val = scan.nextInt(); + if (val <= -1) { + throw new IllegalArgumentException("Valore minore di -1"); + } + System.out.println("Factorial(" + val + ") = " + + MathUtils.factorial(val)); + System.out.print("Another factorial? (y/n) "); + keepGoing = scan.next(); + } catch (IllegalArgumentException e) { + System.out.println(val + " il numero è minore di 0, scrivere un numeroi maggiore o uguale a 0"); } - System.out.println("Factorial(" + val + ") = " - + MathUtils.factorial(val)); - System.out.print("Another factorial? (y/n) "); - keepGoing = scan.next(); } } } \ No newline at end of file