1 package com.homework; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class RandomPhone { 7 //初始化手机前三位 8 static String[] First = {"139","138","137","136","135","134","159","158","157", 9 "150","151","152","188","130","131","132","156","135","133","153","189"};10 //手机号唯一 无序11 static SetphoneSet = new HashSet ();12 //static Set phoneSet = new HashSet ();13 14 //getNumber方法来获得数字15 public static int getNumber(int start,int end) {16 int sum = (int)(Math.random()*(end+1-start)+start);17 return sum;18 }19 public static void main(String[] args) {20 String first = null;21 String second = null;22 String third = null;23 24 25 26 for(;phoneSet.size() <20;) {27 //手机号前三位28 int index = getNumber(0,First.length-1);29 first = First[index];30 // substring 的作用是从指定位置开始字符串31 //生成手机剩下的8位号码32 second = String.valueOf(getNumber(0,99999)+100000);33 third = String.valueOf(getNumber(0,999)+1000);34 // second = String.valueOf(getNumber(0,99999)+100000).substring(0);35 // third = String.valueOf(getNumber(0,999)+1000).substring(0);36 String ch = first+second+third;37 phoneSet.add(ch);38 }39 int i = 0;40 for(String sc :phoneSet) {41 System.out.println("第"+(++i)+"的手机号是"+sc);42 }43 }44 }