caesar c# 输出数字而不是移位的字符
caesar c# outputs numbers instead of shifted characters
为什么这个程序输出的是数字而不是移位的字符?我不明白这段代码有什么问题,我忘了添加什么?任何答案表示赞赏。
static void Main(string[] args)
{
string key;
string text = string.Empty;
int int_key;
Console.Write("Enter a key: ");
key = Console.ReadLine();
if(key == "2")
{
Console.Write("String you want to encrypt: ");
text = Console.ReadLine();
}
else
{
Console.WriteLine("Please enter a valid key: ");
}
int_key = int.Parse(key);
for(int i = 0; i < text.Length; i++)
{
if(char.IsUpper(text[i]))
{
Console.Write("Encrypted string: " + (((char)text[i] + int_key) - 65)% 26 + 65);
}
else
{
Console.WriteLine("Encrypted string: " + (((char)text[i] + int_key) - 97)% 26 + 97);
}
}
Console.ReadKey();
}
Console.Write("Encrypted string: " + (((char)text[i] + int_key) - 65)% 26 + 65);
更有可能是:
Console.Write("Encrypted string: " + (char)(((text[i] + int_key) - 65)% 26 + 65));
// |||_________________| | |
// ||________________________| |
// |___________________________________|
为什么这个程序输出的是数字而不是移位的字符?我不明白这段代码有什么问题,我忘了添加什么?任何答案表示赞赏。
static void Main(string[] args)
{
string key;
string text = string.Empty;
int int_key;
Console.Write("Enter a key: ");
key = Console.ReadLine();
if(key == "2")
{
Console.Write("String you want to encrypt: ");
text = Console.ReadLine();
}
else
{
Console.WriteLine("Please enter a valid key: ");
}
int_key = int.Parse(key);
for(int i = 0; i < text.Length; i++)
{
if(char.IsUpper(text[i]))
{
Console.Write("Encrypted string: " + (((char)text[i] + int_key) - 65)% 26 + 65);
}
else
{
Console.WriteLine("Encrypted string: " + (((char)text[i] + int_key) - 97)% 26 + 97);
}
}
Console.ReadKey();
}
Console.Write("Encrypted string: " + (((char)text[i] + int_key) - 65)% 26 + 65);
更有可能是:
Console.Write("Encrypted string: " + (char)(((text[i] + int_key) - 65)% 26 + 65));
// |||_________________| | |
// ||________________________| |
// |___________________________________|