1.利用Syetem.in.read()方法接收一个键盘输入的字符
图1
图2
图3
public abstract int read()
throws IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
A subclass must provide an implementation of this method.
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if an I/O error occurs.
代码:
//如果输入一个英文字符串或英文标点,则只接收第一个字母
//如果是一个汉字或者汉语标点,只接收第一个字的一半编码
System.in.read();//返回int型
2.利用BufferedReader.
图1
图2
图3
代码:
//2.利用缓冲读取器BufferedReader整行读取输入流(字符、数组、行)
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
//InputStreamReader返回reader型//readLine返回string型
3.通过喜闻乐见的Scanner类
-还可以接收流、文件、通道、指定的源
图1
图2
代码:
Scanner scanner=new Scanner(System.in);//System.in返回inputstream型
scanner.nextLine();//返回string型