【问题标题】:writing console output in java用java编写控制台输出
【发布时间】:2016-12-03 06:23:57
【问题描述】:
class HelloWorld {
public static void main(String args[]) {
int b;
b = 'A';
System.out.write(b);
System.out.write('\n');
System.out.write(97);
System.out.write('\n');
System.out.write(1889); 
System.out.write('\n');
}
}

这个程序的输出是

A
a
a

以下行如何产生 a 作为输出。

System.out.write(1889);

【问题讨论】:

    标签: java console output


    【解决方案1】:

    因为1889 % 256 = 97。 ASCII 字符有 256 个,所以mod operator 用于获取有效字符。

    【讨论】:

      【解决方案2】:

      根据this answer System.out.write(int) 以系统相关的方式将最低有效字节写入输出。在您的情况下,系统决定将其写为字符。

      1889 == 0000 0111 0110 0001
        97 == 0000 0000 0110 0001
      

      两个数字的最右边八位字节相同。正如@Cricket 所提到的,这与取您传入的数字的模数和 256 基本相同。

      【讨论】:

        猜你喜欢
        • 2011-09-26
        • 2018-04-23
        • 1970-01-01
        • 1970-01-01
        • 2017-09-30
        • 2023-03-31
        • 1970-01-01
        • 2020-04-10
        • 1970-01-01
        相关资源
        最近更新 更多