java代码产生随机汉字 java代码产生随机汉字的方法

java中的数字,字母,汉字如何随机生成

数字,字母比较简单。

创新互联是一家专业提供镇雄企业网站建设,专注与成都网站设计、网站建设、H5高端网站建设、小程序制作等业务。10年已为镇雄众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

//手写的,省略了一部分String szzm = "0123...789abc...xyzABC...XYZ"; StringBuilder bu = new StringBuilder();for(int i = 0;i6;i++){ bu.append(szzm.charAt(new Random().nextInt(szzm.length())));}System.out.println(bu.toString());

汉字复杂一些。我的方法是数字区间,19968~40869 之间 int 转char 都会变成汉字。

不过这个范围有点大,2w多汉字,有繁体的,还有生僻字。 如果不怕麻烦,汉字也可和数字一样,弄个汉字表。

StringBuilder bu = new StringBuilder();int base = 19968;int qujian = 40869 - 19968;for(int i = 0;i6;i++){ int rand = base + new Random().nextInt(qujian); bu.append((char)rand);}System.out.println(bu.toString());

JAVA如何编写一个可实现随机显示文字的代码

使用随机数 Math.random()

然后比如随机数范围你设置为0~2

那么随机数=0的时候你就输出System.out.println("1");

那么随机数=1的时候你就输出System.out.println("2");

类推

然后你就可以写一个循环每次生成一个随机数,然后根据随机数来显示

如何在java中随机生成常用汉字

/**

* 原理是从汉字区位码找到汉字。在汉字区位码中分高位与底位, 且其中简体又有繁体。位数越前生成的汉字繁体的机率越大。

* 所以在本例中高位从171取,底位从161取, 去掉大部分的繁体和生僻字。但仍然会有!!

*

*/

@Test

public void create() throws Exception {

String str = null;

int hightPos, lowPos; // 定义高低位

Random random = new Random();

hightPos = (176 + Math.abs(random.nextInt(39)));//获取高位值

lowPos = (161 + Math.abs(random.nextInt(93)));//获取低位值

byte[] b = new byte[2];

b[0] = (new Integer(hightPos).byteValue());

b[1] = (new Integer(lowPos).byteValue());

str = new String(b, "GBk");//转成中文

System.err.println(str);

}

/**

* 旋转和缩放文字

* 必须要使用Graphics2d类

*/

public void trans(HttpServletRequest req, HttpServletResponse resp) throws Exception{

int width=88;

int height=22;

BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);

Graphics g = img.getGraphics();

Graphics2D g2d = (Graphics2D) g;

g2d.setFont(new Font("黑体",Font.BOLD,17));

Random r = new Random();

for(int i=0;i4;i++){

String str = ""+r.nextInt(10);

AffineTransform aff = new AffineTransform();

aff.rotate(Math.random(),i*18,height-5);

aff.scale(0.6+Math.random(), 0.6+Math.random());

g2d.setTransform(aff);

g2d.drawString(str,i*18,height-5);

System.err.println(":"+str);

}

g2d.dispose();

ImageIO.write(img, "JPEG",resp.getOutputStream());

}

java 随机输出10个汉字中的一个,新手求代码

import java.util.Random;

public class Demo {

public static void main(String[] args) {

String[] str ={"一","二","三","四","五","六","七","八","九","十"};

//演示汉字,里面的汉字自己更改

Random r= new Random();

int i = r.nextInt(10);

System.out.println("随机产生的第"+(i+1)+"位数:"+str[i]);

}

}


网站栏目:java代码产生随机汉字 java代码产生随机汉字的方法
分享网址:http://scjbc.cn/article/hjodjd.html

其他资讯