源程序如下:
import java.io.*;
public class Echo {
private static String Hello;public static void main(String[] args) {
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
String s=Hello;
try {
while((s = in.readLine()).length() != 0)
System.out.println(s);
// An empty line terminates the program
} catch(IOException e) {
e.printStackTrace();
}
}

程序中String s=Hello;根据这两条语句:while((s = in.readLine()).length() != 0)   System.out.println(s);
程序应该输出Hello才对,怎么结果是什么都没有呢?

解决方案 »

  1.   

    我想lz这个程序的目的是如果输入字符串就直接输出,否则输出Hello,你如果输入空程序应该输出Hello但是但是你却吧对象s又作为输入流对象,String s=Hello这句当然没有用啦,而且初始化的也不对,以下是我的改正:
            import java.io.*; 
    public class Echo { 
    private static String Hello; public static void main(String[] args) { 
    String inputstr;
    BufferedReader in =new BufferedReader(new InputStreamReader(System.in)); 
    String s="Hello"; 
    try { 
    if((inputstr = in.readLine()).length() != 0) 
    System.out.println(inputstr); 
    else System.out.println(s);
    // An empty line terminates the program 
    } catch(IOException e) { 
    e.printStackTrace(); 



    希望能够帮到lz
      

  2.   

    因为while语句已经执行了s = in.readLine(),输入为空时,即s的值为空。可以试一下这个,只是添了一个sleep()。
    import java.io.*; 
    public class Echo { 
    private static String Hello; public static void main(String[] args) { 
    BufferedReader in =new BufferedReader(new InputStreamReader(System.in)); 
    String s=Hello; 
    try { 
    while((s = in.readLine()).length() != 0)  {}
    System.out.println(s); 
    Thread.sleep(2000);     //放大效果,可以看到实际上输出 了空 , 然后程序结束
    // An empty line terminates the program 
    } catch(IOException e) { 
    e.printStackTrace(); 
    } catch(InterruptedException w){} } 

      

  3.   

    你的程序没错,运行后你输入什么比如 hello,回车后应该打印的就是hello 啊
      

  4.   

    while((s = in.readLine()).length() != 0) 因为当你输入的数据长度大于零的时候一直循环,退出循环需要等于0时,此时输入长度应该就为0了,所以什么也没有输出