从扫描仪获取字符输入 [英] Take a char input from the Scanner

查看:47
本文介绍了从扫描仪获取字符输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来从键盘获取 char 输入.

I am trying to find a way to take a char input from the keyboard.

我尝试使用:

Scanner reader = new Scanner(System.in);
char c = reader.nextChar();

此方法不存在.

我尝试将 c 作为 String.然而,它并不总是适用于所有情况,因为我从我的方法调用的另一个方法需要一个 char 作为输入.因此,我必须找到一种方法来显式地将字符作为输入.

I tried taking c as a String. Yet, it would not always work in every case, since the other method I am calling from my method requires a char as an input. Therefore I have to find a way to explicitly take a char as an input.

有什么帮助吗?

推荐答案

您可以从 Scanner.next 中取出第一个字符:

You could take the first character from Scanner.next:

char c = reader.next().charAt(0);

要使用恰好一个您可以使用的字符:

To consume exactly one character you could use:

char c = reader.findInLine(".").charAt(0);

严格使用一个你可以使用的字符:

To consume strictly one character you could use:

char c = reader.next(".").charAt(0);

这篇关于从扫描仪获取字符输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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