编写一个接收字符并显示其Unicode的程序

时间:2014-10-07 16:04:12

标签: java

import java.util.Scanner;

public class Ex4_9 {

 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("enter character");

    String a = input.nextLine();
    char ch = a.charAt(0);
    if (a.length() == 1){
    System.out.println("The character entered is " + ch);
    System.out.println(" the Unicode for character " + ch + " " + ??);
    }
    else
    System.out.println("complain about the number of characters.");
 }

}

我希望能够输入E和java显示69.我需要填写什么?

2 个答案:

答案 0 :(得分:0)

您想使用codePointAt ...

System.out.println(" the Unicode for character " + a + " " + a.codePointAt(0));

答案 1 :(得分:-1)

只需将ch转换为int即可获取其Unicode值,如

System.out.println(" the Unicode for character " + ch + " " + ((int) ch));

对于评论,这适用于任何 char ,但不适用于任何 Unicode代码点。但是,这个问题要求char的解决方案,我的工作原理。 ch初始化为a.charAt(0),无论如何都不会为代理人工作,所以我不会在downvote中看到任何理由。