为什么该程序输出数字而不是移位的字符?我无法理解此代码中的错误以及忘记添加的内容?任何答案表示赞赏。
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();
}
答案 0 :(得分:2)
Console.Write("Encrypted string: " + (((char)text[i] + int_key) - 65)% 26 + 65);
更可能是:
Console.Write("Encrypted string: " + (char)(((text[i] + int_key) - 65)% 26 + 65));
// |||_________________| | |
// ||________________________| |
// |___________________________________|