Java中的Scanner和nextChar()

Java中的Scanner类 支持nextInt()、nextLong()、nextDouble()等,但没有nextChar()(请参阅 (例如)

null

要读取字符,我们使用 下一步()。查拉特(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

本文由 皮尤斯·古普塔 .如果你喜欢GeekSforgek,并且想贡献自己的力量,你也可以写一篇文章,并将文章邮寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享