Skip to content

Commit

Permalink
M4 Flujos Ciclos y Metodos D7 - Temporizador
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinceto committed May 14, 2024
1 parent 4794ee9 commit c9d04e7
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Vinceto/src/D7/Temporizador.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

0 comments on commit c9d04e7

Please sign in to comment.