我正在尝试创建一个小项目(我是 C# 的新手),有人可以在其中输入密钥和字符串进行加密。但是我有一个逻辑错误,它没有显示。我已尽力解释每一行。
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".ToCharArray(); // so i dont have to make each thig in the array
Console.WriteLine("KeY: "); // ask for key
string key1 = Console.ReadLine(); // store it in a variabe
int key = Convert.ToInt32(key1); // make it a number
Console.WriteLine("Code to encrypt: ");
string code = Console.ReadLine(); // strig that need to be encryot
string EncryptedString = ""; // blank string to insert encrypted letter into
int leng = code.Length;
int i = 0;
while (i > leng)
{
char x = code[i]; // x is after every loop the consequetive letter in the string that needs encrypting
int y = Array.IndexOf(alphabet, x); // finds the index position that "x" is in the alphabet, e.g (a = 0, b = 1)
int z = alphabet[y + key]; // z is the letter index of the letter in alphabet but with the key amount over , imagine the key was 2, e.g (a = c)
string w = Convert.ToString(z); // w is z converted to a string
EncryptedString.Insert(i - 1, w); // is the the position you want to insert the letter, w is the letter you want inserting
i++;
}
Console.WriteLine(EncryptedString);
某处出现逻辑错误,加密后的字符串不会显示
首先从字符串更改为 CharArray
char[] codeArray = code.toCharArray();
int leng = codeArray.Length;
int i = 0;
while (i > leng)
{
char x = codeArray[i]; // x is after every loop the consequetive letter in the string that needs encrypting
试试这个并发布结果