System.in.read实际返回什么? [英] What does System.in.read actually return?

查看:123
本文介绍了System.in.read实际返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做什么:

System.in.read()

返回? 文档说明:

返回:
数据的下一个字节,如果到达流的末尾,则返回-1。

Returns: the next byte of data, or -1 if the end of the stream is reached.

但是例如,如果我输入: 10 我回来了 49 。为什么?

But for example if I enter : 10 I get back 49 . Why is that ?

推荐答案

49 是char的ASCII值 1 。它是第一个字节的值。

49 is the ASCII value of the char 1. It is the value of the first byte.

输入 1 <时产生的字节你的控制台或终端上的kbd> 0 输入包含三个字节 {49,48,10} (在我的Mac上,可能以10,12或12而不是10结束,具体取决于您的系统)。

The stream of bytes that is produced when you enter 10Enter on your console or terminal contains the three bytes {49,48,10} (on my Mac, may end with 10,12 or 12 instead of 10, depending on your System).

所以简单片段的输出

int b = System.in.read();
while (b != -1) {
    System.out.println(b);
    b = System.in.read();
}

输入10并点击输入后,(在我的机器上)

after entering a 10 and hitting enter, is (on my machine)

49
48
10

这篇关于System.in.read实际返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆