import java.util.Random;
public class RandomChinese {
private static Random random = new Random();
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.print(getRandomChinese());
}
}
public static String getRandomChinese() {
String str = "";
int highPos, lowPos;
highPos = (176 + Math.abs(random.nextInt(39)));
lowPos = (161 + Math.abs(random.nextInt(93)));
byte[] bArr = new byte[2];
bArr[0] = (new Integer(highPos)).byteValue();
bArr[1] = (new Integer(lowPos)).byteValue();
try {
str = new String(bArr, "GB2312");
} catch (Exception e) {
e.printStackTrace();
}
return str/*.charAt(0)*/;
}
}
随机生成汉字的方法
最新推荐文章于 2025-10-31 19:54:16 发布
该Java程序使用Random类生成随机的高位和低位字节,结合GB2312编码来创建随机的中文字符。在main方法中,循环10次打印出随机的中文字符。如果遇到异常,程序会打印堆栈跟踪信息。
803





