您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Java實現生成隨機姓名、性別和年齡的示例,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
一、定義實體類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); } } }
測試結果:
看完上述內容,你們對Java實現生成隨機姓名、性別和年齡的示例有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。