Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3.  
  4. /**
  5.  *
  6.  * @author Ángel
  7.  */
  8. public class Excepciones2 {
  9.  
  10.     public static void main(String[] args) {
  11.  
  12.         // Declaración de variables
  13.         Scanner teclado;
  14.         int numero, intentos;
  15.         boolean chivato;
  16.  
  17.         // Inicialización de variables
  18.         teclado = new Scanner(System.in);
  19.         numero = 0;
  20.         intentos = 0;
  21.         chivato = false;
  22.  
  23.         // Bucle que se repetirá mientras el chivato sea falso.
  24.         do {
  25.             try {
  26.                 // Pedimos que ingrese un número por teclado
  27.                 System.out.println("Por favor, introduzca un número comprendidos entre y el 0 y el 100: ");
  28.                 numero = teclado.nextInt();
  29.  
  30.                 if (numero > 0 && numero <= 100) {
  31.                     chivato = true;
  32.                 } else {
  33.                     System.out.println("El número introducido no está "
  34.                             + "comprendido entre el 0 (no incluido) y el 100."
  35.                             + "\n");
  36.                 }
  37.             } catch (ArithmeticException e) {
  38.                 System.err.println("Error al realizar la operación de"
  39.                         + " división.\n");
  40.                 teclado.next();
  41.             } catch (InputMismatchException e) {
  42.                 System.err.println("Error de lectura de datos.\n");
  43.                 teclado.next();
  44.             } catch (Exception e) {
  45.                 System.err.println("Se ha producido el siguiente error: "
  46.                         + e + "\n");
  47.                 teclado.next();
  48.             } finally {
  49.                 intentos++;
  50.             }
  51.         } while (!chivato);
  52.  
  53.         // Mostramos el resultado por pantalla
  54.         System.out.printf("El número introducido es :%d y el número de "
  55.                 + "intentos es: %d.\nFin del programa.", numero, intentos);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement