java程序输出电话号码_【Java】生成随机的手机号码并输出到文件

该Java程序用于生成随机的手机号码,支持中国移动、联通和电信,并可将生成的号码输出到文件。用户输入要生成的手机号数量,程序将随机选择运营商并创建相应数量的号码,最后将号码保存到F:/mobile.txt文件中。
摘要由CSDN通过智能技术生成

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Random;

import java.util.Scanner;

public class Mobile {

//中国移动

public static final String[] CHINA_MOBILE = {

"134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159",

"182", "183", "184", "187", "188", "178", "147", "172", "198"

};

//中国联通

public static final String[] CHINA_UNICOM = {

"130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186", "166"

};

//中国电信

public static final String[] CHINA_TELECOME = {

"133", "149", "153", "173", "177", "180", "181", "189", "199"

};

/**

* 生成手机号

*

* @param op 0 移动 1 联通 2 电信

*/

public static String createMobile(int op) {

StringBuilder sb = new StringBuilder();

Random random = new Random();

String mobile01;//手机号前三位

int temp;

switch (op) {

case 0:

mobile01 = CHINA_MOBILE[random.nextInt(CHINA_MOBILE.length)];

break;

case 1:

mobile01 = CHINA_UNICOM[random.nextInt(CHINA_UNICOM.length)];

break;

case 2:

mobile01 = CHINA_TELECOME[random.nextInt(CHINA_TELECOME.length)];

break;

default:

mobile01 = "op标志位有误!";

break;

}

if (mobile01.length() > 3) {

return mobile01;

}

sb.append(mobile01);

//生成手机号后8位

for (int i = 0; i < 8; i++) {

temp = random.nextInt(10);

sb.append(temp);

}

return sb.toString();

}

public static void main(String[] args) throws IOException {

Scanner scan = new Scanner(System.in);

System.out.println("请输入生成的手机号个数:");

int number = scan.nextInt();

Random random = new Random();

StringBuilder sb = new StringBuilder();

for (int i = 1; i <= number; i++) {

int op = random.nextInt(3);//随机运营商标志位

sb.append(createMobile(op));

if (i % 10 == 0) {

sb.append("\n");

} else {

sb.append("\t");

}

}

//写入文件

FileOutputStream fos = new FileOutputStream(new File("F:/mobile.txt"));

fos.write(sb.toString().getBytes());

fos.close();

System.out.println(number + "个号码生成成功!");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值