generated from Andressep/java-codespace
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
M4 Flujos Ciclos y Metodos D7 - Temporizador
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
|