Java生成隨機姓名、性別和年齡

2020-09-29 16:00:15

一、定義實體類Person,封裝生成的資料

package net.dc.test;

public class Person {
    private String name;
    private String sex;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

二、定義隨機資訊類RandInfo,生成亂資料

package net.dc.test;

import java.util.Random;

public class RandInfo {
    String familyName = "趙錢孫李周吳鄭王馮陳褚衛蔣沈韓楊朱秦尤許何呂施張孔曹嚴華金魏陶姜戚謝鄒喻水雲蘇潘葛奚範彭郎魯韋昌馬苗鳳花方俞任袁柳鮑史唐費岑薛雷賀倪湯滕殷羅畢郝鄔安常樂於時傅卞齊康伍餘元卜顧孟平"
            + "黃和穆蕭尹姚邵湛汪祁毛禹狄米貝明臧計成戴宋茅龐熊紀舒屈項祝董粱杜阮席季麻強賈路婁危江童顏郭梅盛林刁鍾徐邱駱高夏蔡田胡凌霍萬柯盧莫房繆幹解應宗丁宣鄧鬱單杭洪包諸左石崔吉"
            + "龔程邢滑裴陸榮翁荀羊甄家封芮儲靳邴鬆井富烏焦巴弓牧隗山谷車侯伊寧仇祖武符劉景詹束龍葉幸司韶黎喬蒼雙聞莘勞逄姬冉宰桂牛壽通邊燕冀尚農溫莊晏瞿茹習魚容向古戈終居衡步都耿滿弘國文東毆沃曾關紅遊蓋益桓公晉楚閆";
    String firstName2 = "歐陽太史端木上官司馬東方獨孤南宮万俟聞人夏侯諸葛尉遲公羊赫連澹臺皇甫宗政濮陽公冶太叔申屠公孫慕容仲孫鍾離長孫宇文司徒鮮于司空閭丘子車亓官司寇巫馬公西顓孫壤駟公良漆雕樂正宰父穀梁拓跋夾谷軒轅令狐段幹百里呼延東郭南門羊舌微生公戶公玉公儀樑丘公仲公上公門公山公堅左丘公伯西門公祖第五公乘貫丘公皙南榮東里東宮仲長子書子桑即墨達奚褚師吳銘";
    String girlName = "秀娟英華慧巧美娜靜淑惠珠翠雅芝玉萍紅娥玲芬芳燕彩春菊蘭鳳潔梅琳素雲蓮真環雪榮愛妹霞香月鶯媛豔瑞凡佳嘉瓊勤珍貞莉桂娣葉璧璐婭琦晶妍茜秋珊莎錦黛青倩婷姣婉嫻瑾穎露瑤怡嬋雁蓓紈儀荷丹蓉眉君琴蕊薇菁夢嵐苑婕馨瑗琰韻融園藝詠卿聰瀾純毓悅昭冰爽琬茗羽希寧欣飄育瀅馥筠柔竹靄凝曉歡霄楓芸菲寒伊亞宜可姬舒影荔枝思麗";
    String boyName = "偉剛勇毅俊峰強軍平保東文輝力明永健世廣志義興良海山仁波寧貴福生龍元全國勝學祥才發武新利清飛彬富順信子傑濤昌成康星光天達安巖中茂進林有堅和彪博誠先敬震振壯會思群豪心邦承樂紹功鬆善厚慶磊民友裕河哲江超浩亮政謙亨奇固之輪翰朗伯宏言若鳴朋斌樑棟維啟克倫翔旭鵬澤晨辰士以建家致樹炎德行時泰盛雄琛鈞冠策騰楠榕風航弘";

    public String randFamilyName() {
        String str = "";
        int strLen;
        int randNum = new Random().nextInt(2) + 1;
        int index;
        if (randNum == 1) {
            strLen = familyName.length();
            index = new Random().nextInt(strLen);
            str = String.valueOf(familyName.charAt(index));
        } else {
            strLen = firstName2.length();
            index = new Random().nextInt(strLen);
            if (index % 2 == 0) {
                str = firstName2.substring(index, index + 2);
            } else {
                str = firstName2.substring(index - 1, index + 1);
            }
        }
        return str;
    }

    public String randSex() {
        int randNum = new Random().nextInt(2) + 1;
        return randNum == 1 ? "男" : "女";
    }

    public String randName(String sex) {
        String name = "";
        int randNum = new Random().nextInt(2) + 1;
        int index;
        if (sex.equals("男")) {
            int strLen = boyName.length();
            if (randNum % 2 == 0) {
                index = new Random().nextInt(strLen - 1);
                name = boyName.substring(index, index + randNum).concat("-男");
            } else {
                index = new Random().nextInt(strLen);
                name = boyName.substring(index, index + randNum).concat("-男");
            }
        } else {
            int strLen = girlName.length();
            if (randNum % 2 == 0) {
                index = new Random().nextInt(strLen - 1);
                name = girlName.substring(index, index + randNum).concat("-女");
            } else {
                index = new Random().nextInt(strLen);
                name = girlName.substring(index, index + randNum).concat("-女");
            }
        }
        return name;
    }

    public int randAge() {
        return new Random().nextInt(4) + 18;
    }
}

三、定義測試類TestRand,進行測試

package net.dc.test;

public class TestRand {
    public static void main(String[] args) {
        Person person = new Person();
        RandInfo randInfo = new RandInfo();
        for (int i = 0; i < 10; i++) {

            // 姓氏隨機生成
            String familyName = randInfo.randFamilyName();
            // 名字依託於性別產生
            String randName = randInfo.randName(randInfo.randSex());
            String[] fixed = randName.split("-");

            String name = fixed[0];
            String sex = fixed[1];
            int age = randInfo.randAge();
            person.setName(familyName.concat(name));
            person.setSex(sex);
            person.setAge(age);

            System.out.println(person);
        }
    }
}

測試結果:
在這裡插入圖片描述