TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- Scanner没有直接返会char的方法,不过可以通过多种路径获得char,基本上都是从string转化的.
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- public class GetChar {
- public static void main(String[] args)throws Exception {
- //通过扫描类
- Scanner in = new Scanner(System.in);
- char getChar = in.nextLine().charAt(0);
- System.out.println(getChar);
- //通过缓冲流
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- getChar = reader.readLine().charAt(0);
- System.out.println(getChar);
- //直接输出,通过把字节数组转化为char
- byte[] getCharByByte = new byte[2];
- if(System.in.read(getCharByByte) != -1){
- getChar = new String(getCharByByte).charAt(0);
- System.out.println(getChar);
- }
- }
- }
- 运行:
复制代码 C:java>java GetChar
adaf
a
ggg
g
t44
t C:java>java GetChar
中国
中
国国国
国
国国
国
源码下载:http://file.javaxxz.com/2014/11/5/235848203.zip |
|