从InputStream捕获的值在java中无法正确显示

Sri*_*m M 1 java io

有人可以帮助理解下面的程序.

import java.io.BufferedInputStream;
import java.io.IOException;

public class CmdReader {
    public static void main(String []args) {

        try (BufferedInputStream bis = new BufferedInputStream(System.in)) {
            System.out.print("Enter age: "); 
            int age = bis.read();

            System.out.println("Hello, you are " + age);
        } catch(IOException e) {
            e.printStackTrace();
        }   
    }   
}   
Run Code Online (Sandbox Code Playgroud)

输出:

Enter age: 12
Hello, you are 49
Run Code Online (Sandbox Code Playgroud)

无法理解为什么49代替12代印刷.

非常感谢你的帮助.

Mat*_*ead 6

49是数字的ASCII码1.您的代码只读取第一个字符,并将其分配给a int使用字符的值 - 它不会将其转换为它所代表的数字.您应该考虑使用Scanner或其他一些解析值的方法; BufferedInputStream只读数据,没有别的.