试图找到一种方法让我的代码正确打印出我输入的内容。例如:我输入“♣”,它会弹出“?”。我做了研究,但找不到任何与此相关的内容或有助于按照我想要的方式解决这个问题。 (编辑:正如某些人所说,可能是我的控制台,因为它对 2 个人来说效果相同)
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String capital = "";
int t = 1;
// Asking for input
System.out.println("Enter one character");
capital = in.next();
if (capital.length() > 1) { // If more than one character is entered. Display error.
System.out.print("Error! Entered more than one character");
t = 0;
}
if (capital.length() < 1) {// If less than one character is entered. Display error.
System.out.print("Error! Entered less than one character");
t = 0;
}
if (t == 1) { // If no errors. Proceed to print out outcome
System.out.print("You Entered: ");
System.out.print(capital);
}
}
最佳答案
这里没有任何与 ASCII 相关的内容。字符编码是将数值映射到特定视觉字符的系统。 ASCII 是最受限制的字符编码。它只能表示 127 个唯一值。其中包括标准字符 A-Z、a-z、0-9、基本标点符号等。
Unicode 是一种字符编码,可以编码更多种类的字符,包括“♣”。 Java字符串使用Unicode编码,因此它们可以表示该字符集可以编码的所有字符,这几乎是人类已知的任何字符。这里有一个微妙的问题,即 Unicode 实际上有两个版本:Unicode 16 和 Unicode 32。这是因为即使使用 16 位,世界也没有足够的空间来放置所有存在的字符。因此添加了 Unicode 32。大多数字符都适合 Unicode 16,因此这通常都不是问题。
要打印 Java 字符串中字符的 Unicode 数字表示形式,您可以这样做:
System.out.println("♣".codePointAt(0));
在这种情况下,您将得到:
9827
所以 9827 是“♣”的 Unicode 数字表示形式。
关于java - 如何通过 alt+小键盘仅打印 ascii,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55381538/