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

解决方案 »

  1.   

    输出一下System.in.read() 的值就能看出来了,你回车的时候\r\n有两个字节,加上你的1,就是3个字节,read一次只有一个字节,够读3次的了。
      

  2.   


            while(d > 0)
            {
                d = d + (Math.PI + Math.E) / d;            InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);
                String test = null;
                try {
                    test = br.readLine();
                    while(test!=null){
                     System.out.println(test);
                     System.out.println(d);
                     test = br.readLine();
                    }
                    br.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
      

  3.   

    建议使用java.util.Scanner这个用来获取 控制台的输入
    import java.util.Scanner;public class UnresponsiveUI {

        private volatile double d = 1;
        public UnresponsiveUI() throws Exception
        {
         Scanner s = new Scanner(System.in); 
                System.out.println("请输入数字,回车确定:");          while(d > 0)
            {
             d=s.nextDouble();
                d = d + (Math.PI + Math.E) / d;
                
    //             java.util.Scanner scanner=new Scanner();
                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);
        }}