public class Arr {
public static void main(String[] args) {
char[][] arr = new char[4][];
arr[0] = new char[]{1, 2, 3, 4};
arr[1] = new char[]{12, 13, 14, 5};
arr[2] = new char[]{11, 16, 15, 6};
arr[3] = new char[]{10, 9, 8, 7};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(String.format("%s", arr[i][j]));
if (j == 3) {
System.out.println();
}
}
}
}
}
当我在eclipse中运行程序时,输出是
任何人都可以帮助找到错误的位置吗?
System.out.print(String.format("%s", arr[i][j]));
这将使用它的unicode值将每个char打印为字符串。如果要查看整数输出,请考虑将%s
更改为%d
。
这导致代码:
System.out.print(String.format("%d", (int) arr[i][j]));
或者甚至更好,只需使用:
System.out.print((int) arr[i][j]);
这将直接打印整数值。
您的代码正在打印那些奇怪的正方形,因为这些整数映射到unicode中的那些值。看看qazxsw poi,了解Java使用的unicode转换。
如果要打印实际字符,则必须在ASCII中添加48为“0”,低于48的值是不可打印的。
https://www.ssec.wisc.edu/~tomw/java/unicode.html#x0000
试试这个:
http://www.asciitable.com/