如何在Java中自动将输入字符转换为大写 [英] How to convert input char to uppercase automatically in Java

查看:1276
本文介绍了如何在Java中自动将输入字符转换为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Scanner类来获取输入,并希望在显示时将输入转换为大写字母。这是我的代码

I am using a Scanner class to get the input and want to convert the input to uppercase letter when display it. This is my code

Scanner input = new Scanner(System.in);
System.out.print("Enter a letter: ");
char c = input.next().charAt(0);
Character.toUpperCase(c);

由于我已经将它转换为大写,但输出是像

Since I have convert it to uppercase, but the output is like

input: a
c = A;
output: Enter a letter: a



PS:字母a输入到终端

PS: The letter "a" is what I typed in the terminal

但是我想要显示为大写。如何更改?

However I want to it display as an uppercase one. How can I change it?

推荐答案

toUpperCase 方法不会更改 char (它不能);它返回大写的 char 。更改

The toUpperCase method doesn't change the value of the char (it can't); it returns the uppercased char. Change

Character.toUpperCase(c);

c = Character.toUpperCase(c);

UPDATE

已更新的问题现在表明,大写字符要在打字时打印 。 Java不能这样做,因为Java不控制O / S如何回显用户对屏幕的输入。我的解决方案只会产生额外的输出,即使它是大写的。

The updated question now indicates that the uppercased characters are to be printed as they're typed. Java cannot do that, because Java doesn't control how the O/S echoes user input to the screen. My solution above would only produce additional output, even if it is uppercased.

这篇关于如何在Java中自动将输入字符转换为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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