我给出一个数字作为输入19,我在控制台中得到它49。为什么?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = reader.read();
System.out.println(N);
}
}
使用
readLine()然后转换为字符串Integer.parseInt。read()仅读取 Unicode 中的第一个字符。read() 方法读取单个字符。在您的情况下,该字符是“1”。字符 '1' 在 int 中转换为 49。如果输入 23 或 2865,它仍然只会读取第一个字符 '2',你会得到 int 50。可打印的 ASCII 字符(字符代码 32-127)在这里