Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         foo();
  9.     }
  10.  
  11.  
  12.     static void foo() {
  13.         int src = getProperValue();
  14.         System.out.println("scr = " + src);
  15.  
  16.         int dst = getProperRand();
  17.         int count = 1;
  18.         while (src != dst) {
  19.             dst = getProperRand();
  20.             count++;
  21.         }
  22.  
  23.         System.out.println("count = " + count);
  24.     }
  25.  
  26.     static int getProperRand() {
  27.         int rand = new Random().nextInt(9_000_000);
  28.         int l = 1_000_000;
  29.         return l + rand;
  30.     }
  31.  
  32.     static int getProperValue() {
  33.         System.out.println("Write digit more 1 000 000 and less than 10 000 000");
  34.  
  35.         Scanner scanner = new Scanner(System.in);
  36.  
  37.         String v = scanner.next();
  38.         while (!Optional.ofNullable(v)
  39.                 .filter(Main::isDigit)
  40.                 .isPresent()) {
  41.             System.out.println("Not correct");
  42.             System.out.println("Write digit more 1 000 000 and less than 10 000 000");
  43.             v = scanner.next();
  44.         }
  45.  
  46.         return Integer.valueOf(v);
  47.  
  48.     }
  49.  
  50.     static boolean isDigit(String s) {
  51.         Pattern p = Pattern.compile("^\\d{7}$");
  52.         Matcher m = p.matcher(s);
  53.  
  54.         return m.matches();
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement