|
| 1 | +package j0061_T10_VT07_Ejercicio_12C_ArithmeticException; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + public static void main(String[] args) { |
| 7 | + int[] ints = new int[5]; |
| 8 | + Scanner scanner = new Scanner(System.in); |
| 9 | + int i; |
| 10 | + |
| 11 | + do { |
| 12 | + System.out.println("Introduce otro número. Para salir, introduce -1:"); |
| 13 | + try { |
| 14 | + if (!scanner.hasNextInt()) { |
| 15 | + throw new IllegalArgumentException("Entrada no válida. Debes introducir un número."); |
| 16 | + } |
| 17 | + |
| 18 | + i = scanner.nextInt(); |
| 19 | + |
| 20 | + if (i == -1) { |
| 21 | + break; // Salir del bucle si el usuario introduce -1 |
| 22 | + } |
| 23 | + |
| 24 | + if (i > 4) { // Cambiar la validación de rango a <= 4 porque el array tiene índices de 0 a 4 |
| 25 | + throw new ArithmeticException("Número demasiado grande. Debes introducir un número entre 0 y 4."); |
| 26 | + } |
| 27 | + |
| 28 | + ints[i] = i; // Asignar el número al array |
| 29 | + |
| 30 | + } catch (ArithmeticException ex) { |
| 31 | + System.out.println("Error aritmético: " + ex.getMessage()); |
| 32 | + i = 0; // Asegurar que `i` tiene un valor válido para no afectar el flujo |
| 33 | + } catch (IndexOutOfBoundsException ex) { |
| 34 | + System.out.println("Excepción fuera de índice: " + ex.getMessage()); |
| 35 | + i = 0; // Misma precaución que antes |
| 36 | + } catch (IllegalArgumentException ex) { |
| 37 | + System.out.println("Error: " + ex.getMessage()); |
| 38 | + scanner.next(); // Consumir el token no válido para evitar un bucle infinito |
| 39 | + i = 0; |
| 40 | + } finally { |
| 41 | + System.out.println("Última entrada procesada."); |
| 42 | + } |
| 43 | + } while (true); |
| 44 | + |
| 45 | + scanner.close(); |
| 46 | + |
| 47 | + System.out.println("Números introducidos en el array:"); |
| 48 | + for (int j = 0; j < ints.length; j++) { |
| 49 | + System.out.println("Índice " + j + ": " + ints[j]); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +/* |
| 56 | +*** ENTRADA VALIDA: |
| 57 | +Introduce otro número. Para salir, introduce -1: |
| 58 | +2 |
| 59 | +Última entrada procesada. |
| 60 | +Introduce otro número. Para salir, introduce -1: |
| 61 | +4 |
| 62 | +Última entrada procesada. |
| 63 | +Introduce otro número. Para salir, introduce -1: |
| 64 | +-1 |
| 65 | +Última entrada procesada. |
| 66 | +Números introducidos en el array: |
| 67 | +Índice 0: 0 |
| 68 | +Índice 1: 0 |
| 69 | +Índice 2: 2 |
| 70 | +Índice 3: 0 |
| 71 | +Índice 4: 4 |
| 72 | +
|
| 73 | +*** ENTRADA INVALIDA: |
| 74 | +Introduce otro número. Para salir, introduce -1: |
| 75 | +abc |
| 76 | +Error: Entrada no válida. Debes introducir un número. |
| 77 | +Última entrada procesada. |
| 78 | +Introduce otro número. Para salir, introduce -1: |
| 79 | +5 |
| 80 | +Error aritmético: Número demasiado grande. Debes introducir un número entre 0 y 4. |
| 81 | +Última entrada procesada. |
| 82 | +Introduce otro número. Para salir, introduce -1: |
| 83 | +-1 |
| 84 | +Última entrada procesada. |
| 85 | +Números introducidos en el array: |
| 86 | +Índice 0: 0 |
| 87 | +Índice 1: 0 |
| 88 | +Índice 2: 0 |
| 89 | +Índice 3: 0 |
| 90 | +Índice 4: 0 |
| 91 | +
|
| 92 | + */ |
| 93 | + |
0 commit comments