Main.java 619 B

123456789101112131415161718192021222324252627
  1. import java.util.Scanner;
  2. class Main {
  3. private static final Scanner scanner = new Scanner(System.in);
  4. public static void main(String[] args) {
  5. try {
  6. int number = readPositiveNumber();
  7. System.out.println(number);
  8. } catch (NegativeNumberException e) {
  9. System.out.println(e.getMessage());
  10. }
  11. }
  12. public static int readPositiveNumber() throws NegativeNumberException {
  13. System.out.print("Ingresa un número: ");
  14. int number = scanner.nextInt();
  15. if (number < 0) {
  16. throw new NegativeNumberException("No debe ser un número negativo!!");
  17. }
  18. return number;
  19. }
  20. }