2015-06-20 115 views
0
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(); 
} 

我的输出如下:打印Unicode +位数为Unicode

输入消息:一个 输入码:1个 码:1 一个 一个 \ u0062

我试图在这种情况1中添加CODE并打印b,但我只能将它添加到unicode或ascii,但我不能从那里回到b。型char这里'a'的值和int类型这里1的值的

回答

0

算术加法+产生int类型的值,其在这里包含(Unicode)的字符码为'b',但并不类型char。要获得类型char的值,请使用演员(char)(msg[i]+code)