欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java随机生成一个名字和对应拼音的方法

程序员文章站 2024-03-04 22:28:06
本文实例讲述了java随机生成一个名字和对应拼音的方法。分享给大家供大家参考。具体如下: package com.cn.wangk.util.name; imp...

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