java电话号码生成器

package OutPut;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Phone {
	// 判断是否电话格式
	public static boolean isMobileNO(String mobiles) {

		Pattern pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

		Matcher matcher = pattern.matcher(mobiles);

		return matcher.matches();
	}

	// 返回随机电话号码
	public static String getMobile() {

		while (true) {
			String randomPhone = randomPhone();
			if (Phone.isMobileNO(randomPhone)) {
				return randomPhone;
			}

		}

	}

	// 产生随机电话号码格式数字
	public static String randomPhone() {
		String phone = "1";

		Random random = new Random();
		int nextInt = random.nextInt(3);

		if (nextInt == 0) {
			phone = phone + "3" + Phone.randomNumber();
		} else if (nextInt == 1) {
			phone = phone + "5" + Phone.randomNumber();
		} else {
			phone = phone + "8" + Phone.randomNumber();
		}
		return phone;
	}

	// 生成长度为9的随机数
	public static String randomNumber() {

		Random random = new Random();
		int nextInt = random.nextInt(900000000) + 100000000;
		int abs = Math.abs(nextInt);
		String valueOf = String.valueOf(abs);
		return valueOf;
	}

	public static void main(String[] args) {
		int test = 200;
		while (test > 0) {
			System.out.println(Phone.getMobile());
			test--;
		}
	}
}

Phone.getMobile()返回随机电话号码

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值