Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.InputMismatchException;
- import java.util.Scanner;
- /**
- *
- * @author Ángel
- */
- public class Excepciones2 {
- public static void main(String[] args) {
- // Declaración de variables
- Scanner teclado;
- int numero, intentos;
- boolean chivato;
- // Inicialización de variables
- teclado = new Scanner(System.in);
- numero = 0;
- intentos = 0;
- chivato = false;
- // Bucle que se repetirá mientras el chivato sea falso.
- do {
- try {
- // Pedimos que ingrese un número por teclado
- System.out.println("Por favor, introduzca un número comprendidos entre y el 0 y el 100: ");
- numero = teclado.nextInt();
- if (numero > 0 && numero <= 100) {
- chivato = true;
- } else {
- System.out.println("El número introducido no está "
- + "comprendido entre el 0 (no incluido) y el 100."
- + "\n");
- }
- } catch (ArithmeticException e) {
- System.err.println("Error al realizar la operación de"
- + " división.\n");
- teclado.next();
- } catch (InputMismatchException e) {
- System.err.println("Error de lectura de datos.\n");
- teclado.next();
- } catch (Exception e) {
- System.err.println("Se ha producido el siguiente error: "
- + e + "\n");
- teclado.next();
- } finally {
- intentos++;
- }
- } while (!chivato);
- // Mostramos el resultado por pantalla
- System.out.printf("El número introducido es :%d y el número de "
- + "intentos es: %d.\nFin del programa.", numero, intentos);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement