Advertisement
andresnogales

Helper.java

Sep 1st, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Helper {
  5.    
  6.     public static Scanner scanner = new Scanner(System.in);
  7.     public static Random random = new Random();
  8.  
  9.     private static Integer getInt(String inputMessage, String errorMessage) {
  10.         while(true){
  11.             try {
  12.                 System.out.print(inputMessage);
  13.                 return Integer.parseInt(scanner.nextLine());
  14.             } catch (NumberFormatException e) {
  15.                 System.err.println(errorMessage);
  16.            }
  17.         }
  18.     }
  19.    
  20.     public static Integer getInt(String inputMessage) {
  21.         return getInt(inputMessage, "\nError: el valor ingresado no es un entero");
  22.     }
  23.    
  24.     private static Integer getPositiveInt(String inputMessage, String errorMessge) {
  25.         while (true) {
  26.             int num = getInt(inputMessage);
  27.             if(num > 0) return num;
  28.             System.err.println("\n" + errorMessge);
  29.         }
  30.     }
  31.  
  32.     public static Integer getPositiveInt(String inputMessage){
  33.         return getPositiveInt(inputMessage, "\nError: el valor ingresado no es un entero positivo");
  34.     }
  35.    
  36.     public static int randomInt(int min,int max) {
  37.         return random.nextInt(max - min + 1) + min;
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement