在此例中,您从键盘输入一个字符,然后程序检查输入的字符是否是一个字母。如果输入的字符是字母,则程序检查是大写还是小写。这些检查是使用 IsLetter 和 IsLower(两者均返回 bool 类型)来执行的:
// keyword_bool_2.cs
using System;
public class BoolTest
{
static void Main()
{
Console.Write("Enter a character: ");
char c = (char)Console.Read();
if (Char.IsLetter(c))
{
if (Char.IsLower(c))
{
Console.WriteLine("The character is lowercase.");
}
else
{
Console.WriteLine("The character is uppercase.");
}
}
else
{
Console.WriteLine("Not an alphabetic character.");
}
}
} 输入
X
示例输出
Enter a character: X
The character is uppercase.
Additional sample runs might look as follow:
Enter a character: x
The character is lowercase.
Enter a character: 2
The character is not an alphabetic character.