Advertisement
WallHero

TP03E02HelperClass

Oct 16th, 2020
1,980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Helper {
  4.     public static Scanner scanner = new Scanner(System.in);
  5.    
  6.     public static int forceReadPositiveInteger(String msg)
  7.     {
  8.         int ret = 0;
  9.         while(true)
  10.         {
  11.             try
  12.             {
  13.                 System.out.println(msg);
  14.                 while((ret = Integer.parseInt(scanner.next())) < 1) System.out.println("El número a cargar debe ser positivo.\n"+msg);
  15.                 return ret;
  16.             }catch (Exception e) {
  17.                 System.out.println("Error en la carga.");
  18.             }
  19.         }
  20.     }
  21.    
  22.     public static char forceReadAnLetter(String msg)
  23.     {
  24.         char ret = '\0';
  25.         while(true)
  26.         {
  27.             try
  28.             {
  29.                 String aux;
  30.                 System.out.println(msg);
  31.                 while((aux = scanner.next()).length() > 1 || !Character.isAlphabetic((ret = aux.charAt(0))) ) System.out.println("Se debe cargar una sola letra.\n" + msg);
  32.                 return ret;
  33.             }catch (Exception e) {
  34.                 System.out.println("Error en la carga.");
  35.             }
  36.         }
  37.     }
  38.    
  39.    
  40.     public static int getRandomIntBetweenRange(double min, double max){
  41.         int x = (int) ((int)(Math.random()*((max-min)+1))+min);
  42.         return x;
  43.     }
  44.     public static char generateRandomLetter()
  45.     {
  46.         return ((getRandomIntBetweenRange(0,17)% 2 == 0) ?  (char) (getRandomIntBetweenRange(0, 25)+97): (char) (getRandomIntBetweenRange(0, 25)+65));
  47.     }
  48.    
  49.    
  50.     public static boolean isVowel(char xCh)
  51.     {
  52.         xCh = Character.toLowerCase(xCh);
  53.         return xCh == 'a' || xCh == 'e' || xCh == 'i' || xCh == 'o' || xCh == 'u';
  54.     }
  55.    
  56.     public static boolean isRandom()
  57.     {
  58.         char x = '\0';
  59.         while(true)
  60.         {
  61.             try
  62.             {
  63.                 System.out.println("¿Generar valores aleatorios? (S/N)");
  64.                 x =Character.toUpperCase(scanner.next().charAt(0));
  65.                 if(x == 'S' || x == 'N') return x == 'S';
  66.                 System.out.println("Opción inválida, reintente.");
  67.             }catch (Exception e) {
  68.                 System.out.println("Opción inválida, reintente.");
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement