java 生成汉字_java中产生随机汉字(推荐)

4e00-9fa5编码范围,这个范围里应该都是中文。。。

引用楼主 hyb2008dxy 的帖子:

java中如何产生一个随机的汉字,不用预存在数组的方式来做的话,有没有其他方法?

答:参考代码如下:Java code//在0x4e00---0x9fa5之间产生一个随机的字符publicstaticchargetRandomChar()

{return(char)(0x4e00+(int)(Math.random()*(0x9fa5-0x4e00+1)));

}

方法二:

给LZ写了一个,不晓得要得不?

import java.util.Date;

import java.util.Random;

public class Chinese {

public String getChinese(long seed) throws Exception {

String str = null;

int highPos, lowPos;

seed = new Date().getTime();

Random random = new Random(seed);

highPos = (176 + Math.abs(random.nextInt(39)));

lowPos = 161 + Math.abs(random.nextInt(93));

byte[] b = new byte[2];

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

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

str = new String(b, "GB2312");

return str;

}

public static String get300Chinese() throws Exception {

Chinese ch = new Chinese();

String str = "";

for (int i = 300; i > 0; i--) {

str = str + ch.getChinese(i);

}

System.out.println(str);

return str;

}

public static void main(String[] args) throws Exception {

get300Chinese();

}

}

方法三:Java codepublicclassTest1 {publicstaticvoidmain(String[] args) {

RandomHan han=newRandomHan();

System.out.println(han.getRandomHan());

}

}classRandomHan {privateRandom ran=newRandom();privatefinalstaticintdelta=0x9fa5-0x4e00+1;publicchargetRandomHan() {return(char)(0x4e00+ran.nextInt(delta));

}

}

方法四:

import java.util.Date;

import java.util.Random;

public class Chinese {

public String getChinese(long seed) throws Exception {

String str = null;

int highPos, lowPos;

seed = new Date().getTime();

Random random = new Random(seed);

highPos = (176 + Math.abs(random.nextInt(39)));

lowPos = 161 + Math.abs(random.nextInt(93));

byte[] b = new byte[2];

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

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

str = new String(b, "GB2312");

return str;

}

public static String get300Chinese() throws Exception {

Chinese ch = new Chinese();

String str = "";

for (int i = 300; i > 0; i--) {

str = str + ch.getChinese(i);

}

System.out.println(str);

return str;

}

public static void main(String[] args) throws Exception {

get300Chinese();

}

}

转自: http://topic.csdn.net/u/20080507/14/09d90a22-7f46-44ba-9484-d5ca3d3093a4.html

java 随机字母输出到txt文档

2007年03月15日 星期四 16:36import java.io.FileWriter;

public class create {

public static void main(String[] args) {

char s;

char k[] = new char[1000];

double m;

try{

for(int i=0;i<1000;i++){

m = Math.random()*26+97;

s = (char)m;

System.out.print(s);

k[i] = s;

}

FileWriter FW = new FileWriter("c:/123.txt");

FW.write(k);

FW.close();

}

catch(Exception e){

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值