Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Helper {
- public static Scanner scanner = new Scanner(System.in);
- public static Random random = new Random();
- private static Integer getInt(String inputMessage, String errorMessage) {
- while(true){
- try {
- System.out.print(inputMessage);
- return Integer.parseInt(scanner.nextLine());
- } catch (NumberFormatException e) {
- System.err.println(errorMessage);
- }
- }
- }
- public static Integer getInt(String inputMessage) {
- return getInt(inputMessage, "\nError: el valor ingresado no es un entero");
- }
- private static Integer getPositiveInt(String inputMessage, String errorMessge) {
- while (true) {
- int num = getInt(inputMessage);
- if(num > 0) return num;
- System.err.println("\n" + errorMessge);
- }
- }
- public static Integer getPositiveInt(String inputMessage){
- return getPositiveInt(inputMessage, "\nError: el valor ingresado no es un entero positivo");
- }
- public static int randomInt(int min,int max) {
- return random.nextInt(max - min + 1) + min;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement