java隨機生成一個名字和對應拼音的辦法。本站提示廣大學習愛好者:(java隨機生成一個名字和對應拼音的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java隨機生成一個名字和對應拼音的辦法正文
本文實例講述了java隨機生成一個名字和對應拼音的辦法。分享給年夜家供年夜家參考。詳細以下:
package com.cn.wangk.util.name; import java.util.Random; import com.cn.wangk.util.StrTools; public class ChineseName { String[] sName = new String[95]; String[] Name = new String[79]; private String Names; private String Pid; public ChineseName() { sName[0]="白|bai"; sName[1]="白|bai"; sName[2]="蔡|cai"; sName[3]="曹|cao"; sName[4]="陳|chen"; sName[5]="戴|dai"; sName[6]="窦|dou"; sName[7]="鄧|deng"; sName[8]="狄|di"; sName[9]="杜|du"; sName[10]="段|duan"; sName[11]="范|fan"; sName[12]="樊|fan"; sName[13]="房|fang"; sName[14]="風|feng"; sName[15]="符|fu"; sName[16]="福|fu"; sName[17]="高|gao"; sName[18]="古|gu"; sName[19]="關|guan"; sName[20]="郭|guo"; sName[21]="毛|mao"; sName[22]="韓|han"; sName[23]="胡|hu"; sName[24]="花|hua"; sName[25]="洪|hong"; sName[26]="侯|hou"; sName[27]="黃|huang"; sName[28]="賈|jia"; sName[29]="蔣|jiang"; sName[30]="金|jin"; sName[31]="廖|liao"; sName[32]="梁|liang"; sName[33]="李|li"; sName[34]="林|lin"; sName[35]="劉|liu"; sName[36]="龍|long"; sName[37]="陸|lu"; sName[38]="盧|lu"; sName[39]="羅|luo"; sName[40]="馬|ma"; sName[41]="牛|niu"; sName[42]="龐|pang"; sName[43]="裴|pei"; sName[44]="彭|peng"; sName[45]="戚|qi"; sName[46]="齊|qi"; sName[47]="錢|qian"; sName[48]="喬|qiao"; sName[49]="秦|qin"; sName[50]="邱|qiu"; sName[51]="裘|qiu"; sName[52]="仇|qiu"; sName[53]="沙|sha"; sName[54]="商|shang"; sName[55]="尚|shang"; sName[56]="邵|shao"; sName[57]="沈|shen"; sName[58]="師|shi"; sName[59]="施|shi"; sName[60]="宋|song"; sName[61]="孫|sun"; sName[62]="童|tong"; sName[63]="萬|wan"; sName[64]="王|wang"; sName[65]="魏|wei"; sName[66]="衛|wei"; sName[67]="吳|wu"; sName[68]="武|wu"; sName[69]="蕭|xiao"; sName[70]="肖|xiao"; sName[71]="項|xiang"; sName[72]="許|xu"; sName[73]="徐|xu"; sName[74]="薛|xue"; sName[75]="楊|yang"; sName[76]="羊|yang"; sName[77]="陽|yang"; sName[78]="易|yi"; sName[79]="尹|yin"; sName[80]="俞|yu"; sName[81]="趙|zhao"; sName[82]="鐘|zhong"; sName[83]="周|zhou"; sName[84]="鄭|zheng"; sName[85]="朱|zhu"; sName[86]="西方|dongfang"; sName[87]="獨孤|dugu"; sName[88]="慕容|murong"; sName[89]="歐陽|ouyang"; sName[90]="司馬|sima"; sName[91]="西門|ximen"; sName[92]="尉遲|yuchi"; sName[93]="長孫|zhangsun"; sName[94]="諸葛|zhuge"; Name[0]="ai|皚艾哀"; Name[1]="an|安黯谙"; Name[2]="ao|奧傲敖骜翱"; Name[3]="ang|昂盎"; Name[4]="ba|罷霸"; Name[5]="bai|白佰"; Name[6]="ban|斑般"; Name[7]="bang|邦"; Name[8]="bei|北倍貝備"; Name[9]="biao|表標彪飚飙"; Name[10]="bian|邊卞弁忭"; Name[11]="bu|步不"; Name[12]="cao|曹草操漕"; Name[13]="cang|蒼倉"; Name[14]="chang|常長昌敞玚"; Name[15]="chi|遲持池赤尺馳熾"; Name[16]="ci|此次詞茨辭慈"; Name[17]="du|獨都"; Name[18]="dong|東侗"; Name[19]="dou|都"; Name[20]="fa|發乏琺"; Name[21]="fan|范凡反泛帆蕃"; Name[22]="fang|方訪邡昉"; Name[23]="feng|風鳳封豐奉楓峰鋒"; Name[24]="fu|夫符弗芙"; Name[25]="gao|高皋郜鎬"; Name[26]="hong|洪紅宏鴻虹泓弘"; Name[27]="hu|虎忽湖護乎祜浒怙"; Name[28]="hua|化花華骅桦"; Name[29]="hao|號浩皓蒿浩昊灏淏"; Name[30]="ji|積極濟武術疾及基集記紀季繼吉計冀祭際籍績忌寂霁稷玑芨薊戢佶奇诘笈畿犄"; Name[31]="jian|漸劍見建間柬堅儉"; Name[32]="kan|刊戡"; Name[33]="ke|可克科刻珂恪溘牁"; Name[34]="lang|朗浪廊琅阆莨"; Name[35]="li|歷離裡理利立力麗禮黎栗荔瀝栎璃"; Name[36]="lin|臨霖林琳"; Name[37]="ma|馬"; Name[38]="mao|貿冒貌冒懋矛卯瑁"; Name[39]="miao|淼渺邈"; Name[40]="nan|楠南"; Name[41]="pian|片翩"; Name[42]="qian|潛謙倩茜乾虔千"; Name[43]="qiang|強羌锖玱"; Name[44]="qin|親琴欽沁芩矜"; Name[45]="qing|清慶卿晴"; Name[46]="ran|冉然染燃"; Name[47]="ren|仁刃壬仞"; Name[48]="sha|沙煞"; Name[49]="shang|上裳商"; Name[50]="shen|深審神申慎參莘"; Name[51]="shi|師史石時十世士詩始示適炻"; Name[52]="shui|水"; Name[53]="si|思斯絲司祀嗣巳"; Name[54]="song|松頌誦"; Name[55]="tang|堂唐棠瑭"; Name[56]="tong|統通同童彤仝"; Name[57]="tian|天田忝"; Name[58]="wan|萬宛晚"; Name[59]="wei|衛微偉維威韋緯炜惟玮為"; Name[60]="wu|吳物務武午五巫邬兀毋戊"; Name[61]="xi|教師錫洗夕兮熹惜"; Name[62]="xiao|潇蕭笑曉肖霄骁校"; Name[63]="xiong|熊雄"; Name[64]="yang|羊洋陽漾央秧炀飏鴦"; Name[65]="yi|易意依亦伊夷倚毅義宜儀藝譯翼逸憶怡熠沂頤奕弈懿翊轶屹猗翌"; Name[66]="yin|隱因引銀音寅吟胤訚煙蔭"; Name[67]="ying|映英影穎瑛應瑩郢鷹"; Name[68]="you|幽悠右憂猷酉"; Name[69]="yu|漁郁寓於余玉雨語預羽輿育宇禹域譽瑜嶼御渝毓虞禺豫裕钰煜聿"; Name[70]="zhi|制至值知質致智志直治執止置芝旨峙芷摯郅炙雉幟"; Name[71]="zhong|中忠鐘衷"; Name[72]="zhou|周州舟胄繇晝"; Name[73]="zhu|竹主立足朱祝諸珠著竺"; Name[74]="zhuo|卓灼灼拙琢濯斫擢焯酌"; Name[75]="zi|子資茲紫姿孜梓秭"; Name[76]="zong|宗枞"; Name[77]="zu|足族祖卒"; Name[78]="zuo|作左佐笮鑿"; } public void GetName(){ Random random = new Random(); int i = random.nextInt(94); String[] names = StrTools.split(sName[i],"|"); String id = names[0]; String pid = names[1]; String[] mingzi1 = this.GetNameKey(); id = id+mingzi1[1]; pid = pid+mingzi1[0]; if(random.nextInt(2)==1){ mingzi1 = this.GetNameKey(); id = id+mingzi1[1]; pid = pid+mingzi1[0]+","; } this.Names=id; this.Pid=pid; } private String[] GetNameKey(){ Random random = new Random(); int i = random.nextInt(78); String[] names = StrTools.split(Name[i],"|"); String[] mingzi = new String[2]; mingzi[0] = names[0]; mingzi[1] = this.Getkey(names[1]); return mingzi; } private String Getkey(String Name){ if(StrTools.CheckStrNull(Name)) return null; int counti = Name.length(); Random random = new Random(); int i = random.nextInt(counti); return Name.substring(i,i+1); } /** * @param args */ public static void main(String[] args) { // TODO 主動生成辦法存根 //ChineseName chineseName = new ChineseName(); int j=0; for(int i=0;i<10000;i++){ if(j==50){ j=0; System.out.println(); } j++; //System.out.print(chineseName.GetName()+","); } } public String getNames() { return Names; } public void setNames(String names) { Names = names; } public String getPid() { return Pid; } public void setPid(String pid) { Pid = pid; } }
<%@ page contentType="image/jpeg;charset=UTF-8"%> <%@ page import="java.awt.*"%> <%@ page import="java.awt.image.*"%> <%@ page import="java.util.*"%> <%@ page import="javax.imageio.*"%> <%@page import="com.cn.wangk.util.name.ChineseName"%> <%! Color getRandColor(int fc,int bc){//給定規模取得隨機色彩 Random random = new Random(); if(fc>255) fc=255; if(bc>255) bc=255; int r=fc+random.nextInt(bc-fc); int g=fc+random.nextInt(bc-fc); int b=fc+random.nextInt(bc-fc); return new Color(r,g,b); } %> <% out.clear(); out=pageContext.pushBody();//便可(個中out,pageContext均為jsp內置對象!) //設置頁面不緩存 response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); // 在內存中創立圖像 int width=100, height=35; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 獲得圖形高低文 Graphics g = image.getGraphics(); //生成隨機類 Random random = new Random(); // 設定配景色 g.setColor(getRandColor(200,250)); g.fillRect(0, 0, width, height); //設定字體 g.setFont(new Font("黑體",Font.PLAIN,18)); // 隨機發生155條攪擾線,使圖像中的認證碼不容易被其它法式探測到 g.setColor(getRandColor(160,200)); for (int i=0;i<155;i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x,y,x+xl,y+yl); } ChineseName chineseName = new ChineseName(); chineseName.GetName(); String sRand = chineseName.getNames(); String pid = chineseName.getPid(); //g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); g.setColor(Color.BLACK); g.drawString(sRand,5,18); g.setFont(new Font("黑體",Font.PLAIN,12)); g.drawString(pid,5,30); //畫邊框 g.setColor(Color.black); g.drawRect(0,0,width-1,height-1); // 將認證碼存入SESSION 避免被盜取,可使用密鑰加密 session.removeAttribute("gsrand"); session.setAttribute("gsrand",sRand); // 圖像失效 g.dispose(); // 輸入圖像到頁面 ImageIO.write(image, "JPEG", response.getOutputStream());%>
願望本文所述對年夜家的java法式設計有所贊助。