Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Main {
- public static void main(String[] args) {
- foo();
- }
- static void foo() {
- int src = getProperValue();
- System.out.println("scr = " + src);
- int dst = getProperRand();
- int count = 1;
- while (src != dst) {
- dst = getProperRand();
- count++;
- }
- System.out.println("count = " + count);
- }
- static int getProperRand() {
- int rand = new Random().nextInt(9_000_000);
- int l = 1_000_000;
- return l + rand;
- }
- static int getProperValue() {
- System.out.println("Write digit more 1 000 000 and less than 10 000 000");
- Scanner scanner = new Scanner(System.in);
- String v = scanner.next();
- while (!Optional.ofNullable(v)
- .filter(Main::isDigit)
- .isPresent()) {
- System.out.println("Not correct");
- System.out.println("Write digit more 1 000 000 and less than 10 000 000");
- v = scanner.next();
- }
- return Integer.valueOf(v);
- }
- static boolean isDigit(String s) {
- Pattern p = Pattern.compile("^\\d{7}$");
- Matcher m = p.matcher(s);
- return m.matches();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement