| - demand explanation
Required according to user input, generating a phone number corresponding the number of groups
| - realization of ideas
1, through Baidu, access to the corresponding first three digits of the phone number in the real world
2, using Math.random () method, generates eight digits of a phone number
| - Content Code
. 1 Package com.work; 2 . 3 Import java.util.Scanner; . 4 . 5 / ** . 6 * 9527 :: @auther . 7 * @Description: randomly generated telephone number . 8 * @program: shi_yong . 9 * @Create: 2019-07 10:22 -30 10 * / . 11 public class RandomPhoneNum { 12 is static Scanner SC = new new Scanner (the System.in); 13 is 14 public static <lenPhone> void main (String [] args) { 15 // query requires random number number 16 boolean= Boo to true ; . 17 int NUM = 0 ; 18 is do { . 19 // If the user input is not an integer, it requires the user to enter an integer loop 20 is System.out.println ( "How much do you need phone number, enter an integer " ); 21 is String answer = sc.next (); 22 is the try { 23 is // user's input integer into 24 NUM = the Integer.parseInt (answer); 25 // If the conversion is successful, boo it is set to false may be out of the loop 26 is Boo = to false ; 27 } the catch (Exception E) { 28 // if user input is not an integer, throwing an exception, requiring the user to re-enter 29 System.out.println ( "Your input is not an integer, please re-enter" ); 30 } 31 32 } the while (Boo) ; 33 is 34 is System.out.println ( "want your phone number as follows:" ); 35 // to set the number of cycles required by the user number 36 for ( int I = 0; I <NUM; I ++ ) { 37 [ // static method call generated phone number 38 is getPhoneNum (); 39 } 40 } 41 is 42 is // a given static method, generating a single special number 43 public static void getPhoneNum () { 44 is // given real number initial segment, segment number is looked up in the above Baidu real number section 45 String [] = {Start "133", "149", "153", "173" , "177" , 46 , "180", "181", "189", "199", "130", "131", "132" , 47 , "145", "155", "156", "166" , "171", "175", "176", "185", "186", "166", "134", "135" , 48 , "136", "137", "138", "139", "147", "150","151", "152", "157", "158", "159", "172" , 49 , "178", "182", "183", "184", "187", "188", " 198 "," 170 "," 171 " }; 50 51 is // random real number section illustrating the use of an array length property, to obtain the array length, 52 // by Math.random () * the array length to obtain the array index, the random number so that the top three segments 53 is String Start phoneFirstNum = [( int ) (Math.random () * start.length)]; 54 is // the remaining 8 bits out of random 55 String phoneLastNum = "" ; 56 is // define tail number, an 8-bit tail number 57 is Final int LENPHONE 8 = ; 58 // cycle remaining bits 59 for ( int I = 0 ; I <LENPHONE; I ++ ) { 60 // each cycle selected from 0 to 9, a random number 61 is phoneLastNum + = ( int ) (Math.random () * 10 ); 62 } 63 // final mantissa and the number of segments joined 64 String phoneNum + = phoneFirstNum phoneLastNum; 65 System.out.println (phoneNum); 66 } 67 }
| - operating results