如何在 Java 中自动将输入字符转换为大写

How to convert input char to uppercase automatically in Java(如何在 Java 中自动将输入字符转换为大写)
本文介绍了如何在 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);

更新

更新后的问题现在表明大写字符将在输入时在输入时打印.Java 无法做到这一点,因为 Java 不控制操作系统如何将用户输入回显到屏幕上.我上面的解决方案只会产生额外的输出,即使它是大写的.

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 中自动将输入字符转换为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)