
请问高手,我是java新手,我想随机生成一个整数,然后根据汉字编码查询对应汉字,不知如何写代码?
展开全部
楼主来瞧瞧我写的 测试已经通过 代码如下 直接复制就可以运行
import java.util.Random;
public class test {
public static void main(String[] args) {
Random r = new Random();
String random =r.nextInt(9999)+"";
String a = CodeToChinese(random);
System.out.println("随机数是 :"+random);
System.out.println("根据随机的区位码得出的是:"+a);
}
public static String CodeToChinese(String code) {
String Chinese = " ";
for (int i = 0; i < code.length(); i += 4) {
byte[] bytes = new byte[2];
String lowCode = code.substring(i, i + 2);
int tempLow = Integer.parseInt(lowCode, 16);
tempLow += 160;
bytes[0] = (byte) tempLow;
String highCode = code.substring(i + 2, i + 4);
int tempHigh = Integer.parseInt(highCode, 16);
tempHigh += 160;
bytes[1] = (byte) tempHigh;
String chara = new String(bytes);
Chinese += chara;
}
return Chinese;
}
}
import java.util.Random;
public class test {
public static void main(String[] args) {
Random r = new Random();
String random =r.nextInt(9999)+"";
String a = CodeToChinese(random);
System.out.println("随机数是 :"+random);
System.out.println("根据随机的区位码得出的是:"+a);
}
public static String CodeToChinese(String code) {
String Chinese = " ";
for (int i = 0; i < code.length(); i += 4) {
byte[] bytes = new byte[2];
String lowCode = code.substring(i, i + 2);
int tempLow = Integer.parseInt(lowCode, 16);
tempLow += 160;
bytes[0] = (byte) tempLow;
String highCode = code.substring(i + 2, i + 4);
int tempHigh = Integer.parseInt(highCode, 16);
tempHigh += 160;
bytes[1] = (byte) tempHigh;
String chara = new String(bytes);
Chinese += chara;
}
return Chinese;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询