一、定義實體類Person,封裝生成的數據
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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,生成隨機數據
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
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,進行測試
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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); } } } |
測試結果:
到此這篇關于Java生成隨機姓名、性別和年齡的實現示例的文章就介紹到這了,更多相關Java生成隨機姓名、性別和年齡內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/yeyu_xing/article/details/108844330