以下代码将判断键盘输入字符是数字 还是字母:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
using System;
using System.Threading;
public class BoolKeyTest
{
static void Main()
{
repeat:
Console.Write("Enter a character: ");
char c = (char)Console.Read();
if (Char.IsLetter(c))
{
if (Char.IsLower(c))
{
Console.WriteLine($"这时一个小写字母{c}.");
}
else
{
Console.WriteLine($"这时一个小写字母{c}.");
}
}
else if(Char.IsDigit (c))
{
Console.WriteLine($"这是一个数字{c}.");
}
else
{ int s = Convert.ToInt32(c);
Console.WriteLine($"{s} IS Not an alphabetic character.");
}
goto repeat;
//ascii码10是换行,13是回车,所以每次输入字符,按回车键都会识别这两个
Console.ReadLine();
Console.ReadLine();
}
}
/* Sample Output:
汉字提示是字母
Enter a character: X
The character is uppercase.
Enter a character: x
The character is lowercase.
Enter a character: 2
The character is not an alphabetic character.
*/
}