From c9d04e77590314b9a4e621d4871ee4fc0917c833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Bernabe=20Mu=C3=B1oz=20Navarrete?= Date: Mon, 13 May 2024 23:35:52 -0400 Subject: [PATCH] M4 Flujos Ciclos y Metodos D7 - Temporizador --- Vinceto/src/D7/Temporizador.java | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Vinceto/src/D7/Temporizador.java diff --git a/Vinceto/src/D7/Temporizador.java b/Vinceto/src/D7/Temporizador.java new file mode 100644 index 0000000..2b726b5 --- /dev/null +++ b/Vinceto/src/D7/Temporizador.java @@ -0,0 +1,47 @@ +package D7; +import java.util.Scanner; +import java.util.concurrent.TimeUnit; + +public class Temporizador { + + public static void main(String[] args) { + int segundos = 0; + + Scanner sc = new Scanner(System.in); + + do { + try { + System.out.println("Ingrese un nĂºmero para iniciar el contador:"); + String input = sc.nextLine(); + segundos = Integer.parseInt(input); + + if (esEntero(segundos)) { + System.out.println("Comenzando contador regresivo desde " + segundos + " segundos..."); + contadorRegresivo(segundos); + } else { + System.out.println("FALLO"); + } + } catch (NumberFormatException e) { + System.out.println("FALLO"); + } + } while (!esEntero(segundos)); + } + public static boolean esEntero(int num) { + if (num >= Integer.MIN_VALUE && num <= Integer.MAX_VALUE && num >0) { + return true; + } + return false; + } + public static void contadorRegresivo(int segundos) { + try { + for (int i = segundos; i >= 0; i--) { + System.out.println("Segundos restantes: " + i); + TimeUnit.SECONDS.sleep(1); + } + } catch (InterruptedException e) { + System.out.println("El temporizador fue interrumpido."); + Thread.currentThread().interrupt(); + } + } +} +