【发布时间】:2014-10-11 08:04:30
【问题描述】:
好的,所以我制作了一个基本的停止者加密程序,它将每个字符偏移给定数量,这是通过使用字符 ASCII 密钥然后将应该偏移的数量添加到密钥来完成的。
基本上我可以看到为什么我的程序不工作并且没有返回我希望它返回的字符串,这里是加密的伪代码:
`Type in text
Type in encryption key
Display Encrypt(text, key)
function Encrypt(text,key)
For each letter in text
Get its ascii code
add the key to the ascii code
Turn this new ascii code back to a character
Append character to ciphertext string
结束 返回密文`
第一个输入是句子,第二个输入是要偏移的数字
这是我的 C# 代码:
static void Main(string[] args)
{
Console.WriteLine("Write a Sentance!");
string text = Console.ReadLine();
Console.WriteLine("How many characters do you want to ofset it by?");
int key = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(encrypt(text,key));
Console.ReadLine();
}
static string encrypt(string text, int key)
{
string ciphertext = "";
int y = 0;
char[] letters = text.ToCharArray();
for(int x = 0; x <= letters.Length; x++)
{
int AsciiLET = (int)letters[y];
string Asciiletter = (AsciiLET + key).ToString();
ciphertext += Asciiletter;
y++;
}
return ciphertext;
}
【问题讨论】:
-
“我知道为什么我的程序不工作”告诉我们原因。还要发布一些预期的输入和输出。
-
你不使用
key。那是怎么回事? -
这段代码会抛出错误,请注意最好将错误放在以后的问题中。
标签: c# arrays string char ascii