2015-06-20 114 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)