Java 程序检查输入值是否为 ASCII 7 位大写字母
在本文中,我们将探讨如何验证给定字符是否为大写字母,更具体地说是是否在A到Z的范围内。当用户的输入包含某些大写字母或程序需要限制自身为大写字母时,这很有用。我们使用诸如if-else语句、字符比较和条件逻辑等概念来解决此问题。
问题陈述
我们需要编写一个程序来检查字符是否在'A'到'Z'的大写字母范围内。输入
The input will consist of a single character.输出
Character: (given char) TGiven character is in uppercase!
检查字符是否为大写的步骤
以下是在检查字符是否为大写时要遵循的步骤:
- 初始化一个字符变量。
- 使用if-else条件判断字符是否在'A'和'Z'之间。
- 根据结果打印消息。
Java 程序检查字符是否为大写
以下是检查字符是否为大写的 Java 程序 −
public class Demo {
public static void main(String []args) {
char one = 'T';
System.out.println("Character: "+one);
if (one >= 'A' && one <= 'Z') {
System.out.println("Given character is in uppercase!");
} else {
System.out.println("Given character is not in uppercase!");
}
}
}
输出
Character: T Given character is in uppercase!
Java 程序检查字符是否为大写
以下是检查字符是否为大写的 Java 程序 −
public class Demo {
public static void main(String []args) {
char one = 'e';
System.out.println("Character: "+one);
if (one >= 'A' && one <= 'Z') {
System.out.println("Given character is in uppercase!");
} else {
System.out.println("Given character is not in uppercase!");
}
}
}
输出
Character: e Given character is not in uppercase!
代码解释
在此程序中,声明了一个字符变量 one 并分配了一个单个字母,例如“T”或“e”。然后,代码的下一部分使用if 语句来确定字符是否在 A-Z 大写字母范围内。如果是,则程序指出该字符为大写,否则指出情况并非如此。这些简单的方法用于检查给定字符是否为大写字符。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP