leer n con excepcion

This commit is contained in:
PC de clase committed 2026-10-22 13:15:54 +02:00
1 parent 8b3b337b03
commit 42030706b4
1 file changed
+12 -1
+12 -1
View File
@@ -5,7 +5,18 @@ public class EstructuraRepetitiva3_2 {
static void main(String[] args) { static void main(String[] args) {
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
System.out.println("Introduce un número entero: "); 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 // si compuestos[n] == true, el número es compuesto
boolean[] compuestos = new boolean[n + 1]; boolean[] compuestos = new boolean[n + 1];