为什么Scanner类没有nextChar方法? [英] Why doesn't the Scanner class have a nextChar method?

查看:1280
本文介绍了为什么Scanner类没有nextChar方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的是一个好奇心而不是问题......

This is really a curiosity more than a problem...

为什么 Scanner 类没有有一个 nextChar()方法?当你考虑它有 next nextInt nextLine时,它似乎应该是这样的等方法。

Why doesn't the Scanner class have a nextChar() method? It seems like it should when you consider the fact that it has next, nextInt, nextLine etc method.

我意识到你可以简单地做到以下几点:

I realize you can simply do the following:

userChar = in.next().charAt(0);
System.out.println( userChar  );

但为什么没有 nextChar()方法?

推荐答案


原因是Scanner类设计用于读取以空格分隔的标记。它是一个包含底层输入流的便利类。在扫描仪之前,您所能做的就是以单字节读取,如果您想要阅读单词或行,这将是一个巨大的痛苦。使用Scanner,您可以传入System.in,它会执行许多read()操作来为您输入令牌。读取单个字符是一项更基本的操作。
来源

您可以使用(char)System.in.read();

这篇关于为什么Scanner类没有nextChar方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆