Java程序检查输入值是否为ASCII 7位小写字母
在这篇文章中,我们将学习如何 检查输入值是否为ASCII 7位字母小写,在 Java 中。在 ASCII 中,小写字母从字符 'a' 开始,到 'z' 结束。我们将编写一个程序,使用简单的if-else 语句来检查字符是否为小写字母字符。
问题陈述
给定一个字符,我们需要创建一个 Java 程序来检查该字符是否在 ASCII 7 位范围内的小写字母,即它是否介于 'a' 和 'z' 之间。我们还将检查字符是否为大写字母,即它是否介于 'A' 和 'Z' 之间。它将打印该字符是否为小写字母、大写字母或都不是。
输入 1
char one = 'm';输出 1
Character: m
Given character is in lowercase!
输入 2
char one = 'N';输出 2
Character: N
Given character is not in lowercase!
不同的方法
检查小写 ASCII 字母
以下是检查字符是否为小写 ASCII 字母的步骤:
- 定义字符变量并赋值为 'm'。
- 使用 if-else 语句检查字符是否介于 'a' 和 'z' 之间。
- 如果为真,则打印该字符为小写。
示例
下面是一个 Java 程序,用于检查字符是否为小写 ASCII 字母。
public class Demo {
public static void main(String []args) {
char one = 'm';
System.out.println("Character: "+one);
if (one >= 'a' && one <= 'z') {
System.out.println("Given character is in lowercase!");
} else {
System.out.println("Given character is not in lowercase!");
}
}
}
输出
Character: m Given character is in lowercase!
检查大写 ASCII 字母
以下是检查字符是否为大写 ASCII 字母的步骤:
- 定义字符变量并赋值为 'N'。
- 使用 if-else 语句检查字符是否介于 'a' 和 'z' 之间。
- 如果为假,则打印该字符不是小写。
示例
下面是一个 Java 程序,用于检查字符是否为大写 ASCII 字母:
public class Demo {
public static void main(String []args) {
char one = 'N';
System.out.println("Character: "+one);
if (one >= 'a' && one <= 'z') {
System.out.println("Given character is in lowercase!");
} else {
System.out.println("Given character is not in lowercase!");
}
}
}
输出
Character: N Given character is not in lowercase!
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP