当前位置: 代码迷 >> J2SE >> 一个小疑点,大牛小牛都进来看看
  详细解决方案

一个小疑点,大牛小牛都进来看看

热度:126   发布时间:2016-04-24 00:20:16.0
一个小问题,大牛小牛都进来看看
Java code
class UnresponsiveUI{    private volatile double d = 1;    public UnresponsiveUI() throws Exception    {        while(d > 0)        {            d = d + (Math.PI + Math.E) / d;            System.in.read();            System.out.println(d);        }        }}    public static void main(String[] args) throws Exception    {        new UnresponsiveUI(); // Must kill this process        //new ResponsiveUI();        //System.in.read();        //System.out.println(d);    }}

在控制台输入1,为什么一下子打印出来:
6.859874482048838
7.714099220035395
8.473730943053596

------解决方案--------------------
输出一下System.in.read() 的值就能看出来了,

你回车的时候\r\n有两个字节,加上你的1,就是3个字节,read一次只有一个字节,够读3次的了。
  相关解决方案