本文实例讲述了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程序设计有所帮助。