import java.io.*;
public class Test
{
public static void main(String [] args) throws Exception {
System.out.print("Input a char:");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String ch = br.readLine();
//char ch = (char)System.in.read();
System.out.print("hello" + ch + "AB");
System.out.print(ch);
}
}
如果不输入任何数据,直接回车,那么打印出的结果是:helloAB
-------------------------------------------------------------------------------------
public class Test
{
public static void main(String [] args) throws Exception
{
System.out.print("Input a char:");
char ch = (char)System.in.read();
System.out.println("hello" + ch + "AB");
}
}
对与这段代码,不输入任何数据,直接回车,打印的结果是:ABllo
这段代码的结果我可以理解,因为ch只记录了"\r",没有换行。
但是上面那段代码中,ch应该记录下了\r和\n 但为什么打印出来的时候没有换行呢?
我觉得结果应该是
hello
AB
请教一下 这是为什么????
public class Test
{
public static void main(String [] args) throws Exception {
System.out.print("Input a char:");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String ch = br.readLine();
//char ch = (char)System.in.read();
System.out.print("hello" + ch + "AB");
System.out.print(ch);
}
}
如果不输入任何数据,直接回车,那么打印出的结果是:helloAB
-------------------------------------------------------------------------------------
public class Test
{
public static void main(String [] args) throws Exception
{
System.out.print("Input a char:");
char ch = (char)System.in.read();
System.out.println("hello" + ch + "AB");
}
}
对与这段代码,不输入任何数据,直接回车,打印的结果是:ABllo
这段代码的结果我可以理解,因为ch只记录了"\r",没有换行。
但是上面那段代码中,ch应该记录下了\r和\n 但为什么打印出来的时候没有换行呢?
我觉得结果应该是
hello
AB
请教一下 这是为什么????
当输入回车的时候,根本没读取到任何东西,所以就输出helloAB
System.in.read()返回的是int型