Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active May 16, 2021 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/cd73870c247c2c8923fb9b50e91f23b2 to your computer and use it in GitHub Desktop.
Save parzibyte/cd73870c247c2c8923fb9b50e91f23b2 to your computer and use it in GitHub Desktop.
package me.parzibyte;
import java.util.Scanner;
public class Main {
// https://parzibyte.me/blog
public static int solicitarNumeroValido(String mensaje, int minimo, int maximo) {
Scanner s = new Scanner(System.in);
int numero;
while (true) {
System.out.println(mensaje);
if (s.hasNextInt()) {
numero = s.nextInt();
if (numero >= minimo && numero <= maximo) {
return numero;
} else {
System.out.println("Número fuera de rango. Intente de nuevo");
}
} else {
s.next();
}
}
}
// https://parzibyte.me/blog
public static void main(String[] args) {
int numero = solicitarNumeroValido("Ingresa un número entre 1 y 5: ", 1, 5);
System.out.println("El número ingresado es: " + numero);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment