c#从键盘上输入一个小写字符,在屏幕上输出该字符以及它的大写字符和它的ASCII码.

2025-04-15 12:02:27
推荐回答(2个)
回答1:

string s = Console.ReadLine();
if (s.Length != 1)
{
Console.WriteLine("你输入字符长度不正确");
return;
}
if ("abcdefghijklmnopqrstuvwxyz".IndexOf(s) < 0)
{
Console.WriteLine("你输入的不是小写字母");
return;
}
Console.WriteLine("你输入的是" + s);
Console.WriteLine("对应的大写是" + s.ToUpper());
byte[] array = new byte[1]; //定义一组数组array
array = System.Text.Encoding.ASCII.GetBytes(s); //string转换的字母
int asciicode = (short)(array[0]);
string ASCII = Convert.ToString(asciicode); //将转换一的ASCII码转换成string型
Console.WriteLine(s + "的ASCII码是" + ASCII);
Console.ReadKey();

回答2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{

class Program
{
static void Main(string[] args)
{

Console.WriteLine("Please input lower:");
string lower = Console.ReadLine();
string upper = lower.ToUpper();
System.Text.ASCIIEncoding asciiEncoding = new ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(lower)[0];
Console.WriteLine("Upper: " + upper);
Console.WriteLine("ASCII: " + intAsciiCode);
}
}
}