Java 随机生成任意组电话号码过程解析

随机生成任意要求数量的电话号码

package com.work;
 
import java.util.Scanner;
 
/**
 * @auther::9527
 * @Description: 随机生成电话号码
 * @program: shi_yong
 * @create: 2019-07-30 10:22
 */
public class RandomPhoneNum {
  static Scanner sc = new Scanner(System.in);
 
  public static <lenPhone> void main(String[] args) {
    //询问需要多少个随机号码
    boolean boo = true;
    int num = 0;
    do {
      //如果用户输入的不是一个整数,就循环要求用户输入一个整数
      System.out.println("你需要多少组电话号码,请输入一个整数");
      String answer = sc.next();
      try {
        //将用户的输入转化为整数
        num = Integer.parseInt(answer);
        //如果转换成功,boo就设置为false使其可以跳出循环
        boo = false;
      } catch (Exception e) {
        //如果用户输入的不是一个整数,就抛出异常,要求用户重新输入
        System.out.println("你输入的不是一个整数,请重新输入");
      }
 
    } while (boo);
 
    System.out.println("你要的手机号码如下:");
    //将循环次数设置为用户需要的号码的数量
    for (int i = 0; i < num; i++) {
      //调用静态方法生成手机号码
      getPhoneNum();
    }
  }
 
  //定一个静态方法,专门生成单个的号码
  public static void getPhoneNum() {
    //给予真实的初始号段,号段是在百度上面查找的真实号段
    String[] start = {"133", "149", "153", "173", "177",
        "180", "181", "189", "199", "130", "131", "132",
        "145", "155", "156", "166", "171", "175", "176", "185", "186", "166", "134", "135",
        "136", "137", "138", "139", "147", "150", "151", "152", "157", "158", "159", "172",
        "178", "182", "183", "184", "187", "188", "198", "170", "171"};
 
    //随机出真实号段  使用数组的length属性,获得数组长度,
    //通过Math.random()*数组长度获得数组下标,从而随机出前三位的号段
    String phoneFirstNum = start[(int) (Math.random() * start.length)];
    //随机出剩下的8位数
    String phoneLastNum = "";
    //定义尾号,尾号是8位
    final int LENPHONE = 8;
    //循环剩下的位数
    for (int i = 0; i < LENPHONE; i++) {
      //每次循环都从0~9挑选一个随机数
      phoneLastNum += (int) (Math.random() * 10);
    }
    //最终将号段和尾数连接起来
    String phoneNum = phoneFirstNum + phoneLastNum;
    System.out.println(phoneNum);
  }
}
代码简单干净,易于扩展。 随机生成用户名样例: 339063 77188 fengjr1980@yeah.net 97133 xupe19870102@sohu.com 18955990722 2145028 25070167 299966 13329809029 18739552501 77223 725525865 cheng1991 chub1974 13290062609 wangf19750725 caot19850619 545121 qianih19760611@msn.com 15208376363 xieww1979 shixd0904@126.com 18035677437 80892 18967033182 18923271615 15506730128 19741 fangi0713 15829813954 58285 yuanr19730429 shenpk 79607 15430 15226507276 13905108731 71852 qianmp19900629 15578331045 28116 18636868380 41560 zhengk1009@qq.com chuh19901023 269195 5678146 29585 18416167445 konghs1014 13404997586 30505 13647749758 13153649262 18779413966 wangqq1985 18906839566 18915522113 13392180833 1539516 zhaod19960502@ask.com 422424 heee0615 fengc@163.com 39174 xuyv19780415 15463681074 700812 73013 xuh0513 15505384990 hedd19940702 54021 xum19930812 youy1991 qiner 18033283015 yangwc 57195 qinu shenk0513 zhoufg 94597 95435 zhanvn1985 qiankh0104@ask.com kongw1993 zhuhv 15922309734 15015131852 15617928674 13135363801 18249225151 81438 13163635474 18615772400 hes0510 460915 wum19781014 491159 shenh1973 625701 34613 15732006897 fengdk1985 18517435664 13412117745 10157 chuza1993@163.com zhaokk0810@sina.com 34270 7744626 sunb0122 71114 91762 18506579548 yangmy0227 18791566645 13250565847 jiangvk1977 15250922291 37579 wangci1965 wangpq wango19850516 18531237843 18589683471 hegl1990 18714299209 zhant@gmail.com 29016 zhux1969 xiez1113 zhengtw 15490897758 18917969768 18149215132 18608279756 17149 18644736737 zhaov19880816 xuy@aol.com 5854142 18408454618 qinl1988 xubl1990 18852302621 18985792169 42928 18622907192 fanggs19960616@163.com 25150 47585 15086377222 617299 13447565144 668953950 4342797 18638890078 fangiq0615 zhuo19701127@ask.com 421528 15690966759 18058824538 18194138008 15770603106 heag0813 15793392577 470979 18402649812 1791830 18397977938 shukj19960528 87052642 chulu0119 jiangj1986 13210747847 47359 80638214 zhuey0405 94017 13636960571 18939715988 yuani0529@msn.com 58081 caodm1989 18420167788 13951603849 10205 164883 15513277337 29553 15011669462 87997 zhanji 29763568 13032055621 13617051541 lit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值