新聞中心
如何在java中隨機(jī)生成常用漢字
/**
寶清網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),寶清網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為寶清近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的寶清做網(wǎng)站的公司定做!
* 原理是從漢字區(qū)位碼找到漢字。在漢字區(qū)位碼中分高位與底位, 且其中簡(jiǎn)體又有繁體。位數(shù)越前生成的漢字繁體的機(jī)率越大。
* 所以在本例中高位從171取,底位從161取, 去掉大部分的繁體和生僻字。但仍然會(huì)有!!
*
*/
@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");//轉(zhuǎn)成中文
System.err.println(str);
}
/**
* 旋轉(zhuǎn)和縮放文字
* 必須要使用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如何編寫一個(gè)可實(shí)現(xiàn)隨機(jī)顯示文字的代碼
使用隨機(jī)數(shù) Math.random()
然后比如隨機(jī)數(shù)范圍你設(shè)置為0~2
那么隨機(jī)數(shù)=0的時(shí)候你就輸出System.out.println("1");
那么隨機(jī)數(shù)=1的時(shí)候你就輸出System.out.println("2");
類推
然后你就可以寫一個(gè)循環(huán)每次生成一個(gè)隨機(jī)數(shù),然后根據(jù)隨機(jī)數(shù)來顯示
java 隨機(jī)生成漢字昵稱,急求
public?class?Test?{
public?static?void?main(String[]?args)?{
RandomHan?han?=?new?RandomHan();
System.out.print(han.getRandomHan());
}
}
class?RandomHan?{
private?Random?ran?=?new?Random();
private?final?static?int?delta?=?0x9fa5?-?0x4e00?+?1;
public?char?getRandomHan()?{
return?(char)(0x4e00?+?ran.nextInt(delta));?
}
}
隨機(jī)生成漢字的代碼,希望能幫助到您。
網(wǎng)頁標(biāo)題:java隨機(jī)漢字代碼 java 隨機(jī)數(shù)生成字母
標(biāo)題來源:http://www.ef60e0e.cn/article/ddipogh.html