public static void main (String[] args) {
char[] msg;
int code;
int i;
String newMsg;
msg = getMsg(); // Read the message from keyboard
code = getCode();
System.out.println("Code : "+code);
for (i=0; i<msg.length; i++){
System.out.println(msg[i]);
System.out.println(Character.toString((char)msg[i]));
newMsg = ( "\\u" + Integer.toHexString(msg[i] + code | 0x10000).substring(1));
System.out.println (String.valueOf(msg[i] + code ));
System.out.println (newMsg);
}
public static int getCode(){
int code=0;
System.out.print("Input Code: ");
Scanner input = new Scanner(System.in);
return input.nextInt();
}
public static char[] getMsg(){
String myMessage;
System.out.print("Input Message: ");
Scanner input = new Scanner(System.in);
myMessage = input.nextLine();// Read a line of message
return myMessage.toCharArray();
}我的输出如下:
输入信息:a输入代码:1代码:1 a a 98 \u0062
我试图将这种情况下的代码1添加到a中并打印b,但我只能将它添加到unicode或ascii中,但我不能从那里返回到b。
发布于 2015-06-21 09:27:38
将char类型的值(这里是'a' )和int类型的值(这里是1)进行算术加法运算,得到类型为int的值,其中包含'b'的(Unicode)字符代码,但不是char类型。要获取char类型的值,请使用强制转换(char)(msg[i]+code)。
https://stackoverflow.com/questions/30959089
复制相似问题