关于realLine()

来源:百度知道 编辑:UC知道 时间:2024/07/05 04:35:03
public class 控制台输入 {
public static String readLine(){
StringBuffer response=new StringBuffer;
try{
BufferedInputStream buff=new BufferedInputStream
(System.in);
int in=0;
char inChar;
do{
in=buff.read();
inChar=(char)in;
}while((in!=-1)&(inChar!='\n'));
buff.close();
return response.toString();
}catch(IOException e){
System.out.println("Exception: "+e.getMessage());
return null;
}
}
public static void main(String args[]){
System.out.print("What is your name?");
String input=控制台输入.readLine();
System.out.print("Hello, "+input);
System.out.print("Hello, "+控制台输入.readLine());

修改如下: 原因很简单,你没有把输入的东西添加到response中。
public static String readLine() {
StringBuffer response = new StringBuffer();
try {
BufferedInputStream buff = new BufferedInputStream(System.in);
int in = 0;
char inChar;
do {
in = buff.read();
inChar = (char) in;
response.append(inChar);
} while ((in != -1) & (inChar != '\n'));
return response.toString();
} catch (IOException e) {
System.out.println("Exception: " + e.getMessage());
return null;
}
}

正解,StringBuffer response=new StringBuffer; response根本没有在循环体中参与运算,当然不会有值了