在Java中的扫描器类Scanner和nextChar()
Java中的扫描器类支持nextInt(),nextLong(),nextDouble()等。但是没有nextChar()
要读取char,我们使用next()。charAt(0)。next()函数返回输入中的下一个标记/字符作为字符串,并且charAt(0)函数返回该字符串中的第一个字符。
// Java program to read character using Scanner
// class
import java.util.Scanner;
public class ScannerDemo1
{
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// Character input
char c = sc.next().charAt(0);
// Print the read value
System.out.println("c = "+c);
}
}
输入:
G
输出:
c = g