目 录CONTENT

文章目录

随机生成手机号码

芈亓
2022-03-14 / 0 评论 / 2 点赞 / 692 阅读 / 2,091 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-04-12,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

下面是纯代码,复制粘贴适当更改一下路径即可使用。

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
 
 
public class Test02 {
    private static String[] str={"180","189","187","188","181","182","165","134","135"
            ,"136","137","138","139","150"
            ,"151","152","157","158","159"
            ,"130","131","132","155"
            ,"156","133","153"};
    private static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("欢迎使用手机号码生成,下面是使用说明书");
        while (true){
            System.out.println("[1]生成手机号,[2]退出");
            int num=scanner.nextInt();
            if (num==1){
                System.out.println("请输入您想生成的手机号码个数:");
                HashSet<String> set=generateRandomMobileNumber(scanner.nextInt());
                System.out.println("是否需要将生成的手机号输出至指定目录? 是 or 否 ");
                try {
                    judged(scanner.next(),set);
                    System.out.println("请选择接下来的操作");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else if (num==2){
                System.exit(0);
            }else {
                System.out.println("输入错误,请仔细阅读使用说明。");
            }
        }
 
    }
 
    private static void judged(String s,HashSet<String> set) throws IOException {
        if ("是".equals(s)){
            System.out.println("请输入指定目录,例:D:\\temp.txt");
            output(scanner.next(),set);
        }else if ("否".equals(s)){
            return;
        }else{
            System.out.println("输入错误,请输入 是 or 否 ");
        }
    }
 
    private static void output(String catalog,HashSet<String> set) throws IOException {
        BufferedWriter bufferedWriter =new BufferedWriter(new FileWriter(catalog,true));
        for (String s:set) {
            bufferedWriter.write("蚂蚁森林:"+s+"\n");
        }
        bufferedWriter.flush();
        bufferedWriter.close();
    }
 
    private static HashSet<String> generateRandomMobileNumber(int num) {
        Random random=new Random();
        HashSet<String> set=new HashSet<>();
        while (set.size()<num){
            String s=str[random.nextInt(str.length)]+"";
            for (int i = 0; i <8 ; i++) {
                s+=random.nextInt(10);
            }
            set.add(s);
        }
        return set;
    }
}
2

评论区