在java.util.Scanner类中有next(),nextLine(),nextInt( ),nextLong( )等获取各种类型输入的方法
但并没有nextChar( )方法来获取输入的单个字符
我们可以使用 next( ).charAt(0)方法 来获取输入的单个字符
基本语法如
char getChar = scanner.next().charAt(0);
next()方法读取输入的下一个完整标记,然后你可以通过charAt(0)获取这个字符串的第一个字符。
在java.util.Scanner类中有next(),nextLine(),nextInt( ),nextLong( )等获取各种类型输入的方法
但并没有nextChar( )方法来获取输入的单个字符
我们可以使用 next( ).charAt(0)方法 来获取输入的单个字符
基本语法如
char getChar = scanner.next().charAt(0);
next()方法读取输入的下一个完整标记,然后你可以通过charAt(0)获取这个字符串的第一个字符。