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

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

问题描述

我试图找到一种从键盘输入 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 作为输入。因此,我必须找到一个明确接受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天全站免登陆