FacundoCruz

Helper

Oct 29th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Helper {
  5.     //----------------Objetos para toda la clase Tp04----------------//
  6.     public static Scanner sc = new Scanner(System.in);
  7.     static Random random=new Random();
  8.    
  9.   //----------------------- Validar Entradas-----------------------//
  10.    
  11.     static int numeroEntero(String mensaje) {           //Módulo de validación de un entero.
  12.         int salida=0;
  13.         boolean valido;
  14.         do {
  15.             System.out.print(mensaje);
  16.             try {
  17.             return Integer.parseInt(sc.nextLine());
  18.            } catch (NumberFormatException e) {         
  19.                 valido=false;
  20.            } //Fin try-catch.
  21.         } while (valido==false);
  22.         return salida;                
  23.     } //Fin numeroEntero.
  24.    
  25.     static int numeroEnteroPositivo(String mensaje){    //Módulo dedicado a reconocer un número natural.
  26.         int salida;
  27.         do {
  28.             salida=numeroEntero(mensaje);
  29.         } while (salida<1);
  30.         return salida;
  31.     } //Fin numeroEnteroPositivo.
  32.    
  33.     static int entero(String mensaje) {                     //Módulo dedicado a obtener un entero iterador.        
  34.         @SuppressWarnings("resource")
  35.         Scanner scan = new Scanner(System.in);
  36.         System.out.print(mensaje+" ");
  37.         do {
  38.             if (scan.hasNext()) {
  39.                 if (scan.hasNextInt()) {
  40.                     return scan.nextInt();
  41.                 } else {
  42.                     scan.next();
  43.                     System.out.print("Ingrese un número valiado....");
  44.                 }
  45.             } else {
  46.                 System.out.println("Recibido EOF (forzamos 0)");
  47.         return 0;
  48.             }
  49.         } while (true);      
  50.         } //Fin obten_int.
  51.     //----------------------- Validar Datos-----------------------//
  52.    
  53.     static boolean dentroIntervalo(int numero,int minimo, int maximo) {     //Módulo dedicado a validar que ingrese 1 o 2.
  54.         boolean fuera=false;
  55.         if(numero < minimo || numero > maximo ){
  56.             System.out.println("Ingrese "+ minimo+" como mínimo o "+maximo +"  como máximo...");
  57.             fuera = true;
  58.         }return fuera;        
  59.     } //Fin rangodeInt.
  60.    
  61.     static boolean naturalesCero(int numero) {                  //Módulo de validación de un número no negativo.
  62.         boolean band=false;
  63.         if(numero <=-1 ){
  64.             System.out.println("Ingrese un entero no negativo...");
  65.             band = true;
  66.         }return band;  
  67.     }  //Fin de_0_a_mas.
  68.    
  69.     static int generadorAleatorio(int limite) {                         //Módulo generador de random con limite de parametro.
  70.         int numero;
  71.         Random random=new Random();
  72.         numero=random.nextInt(limite);
  73.         return numero;
  74.     }  //Fin random.
  75. } //Fin class Helper
Add Comment
Please, Sign In to add comment