From 42030706b461e991e24a36eceafe4225247bd057 Mon Sep 17 00:00:00 2001 From: PC de clase Date: Thu, 22 Oct 2026 13:15:54 +0200 Subject: [PATCH] leer n con excepcion --- src/EstructuraRepetitiva3_2.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/EstructuraRepetitiva3_2.java b/src/EstructuraRepetitiva3_2.java index f13affa..40b3d6b 100644 --- a/src/EstructuraRepetitiva3_2.java +++ b/src/EstructuraRepetitiva3_2.java @@ -5,7 +5,18 @@ public class EstructuraRepetitiva3_2 { static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Introduce un número entero: "); - int n = sc.nextInt(); + + int n = 0; + try{ + n = sc.nextInt(); + } catch (Exception e) { + System.out.println("La lectura de input ha lanzado la exception" + e + "\n"); + } + + if (n <= 4){ + System.out.println("El numero tiene que ser mayor que 4"); + return; + } // si compuestos[n] == true, el número es compuesto boolean[] compuestos = new boolean[n + 1];