我正在創建一個凱撒密碼解密/加密程序。我的程序從.txt文件讀取,顯示26個不同班次的所有輸出。將循環輸出保存在文件中僅保存最後一次迭代
我在將輸出保存爲.txt時遇到問題:只有最終迭代的輸出是保存到文件的內容。我想保存所有26次迭代。
查看圖片的例子。
該循環,並寫入到文件中的代碼:
static void decryption()
{ Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine ("\n*********************************** Decryption *********************************");
Console.ResetColor();
//pulls getPath from varables class
string path = globalVars.getPath();
string encrypted_text = System.IO.File.ReadAllText(path); //String variable that contains the text from a file. To get the text, the method in a class SystemIO is ran to read the text. It expects a parameter, which is a file directory.
string decoded_text = " ";
int shift = 0;
char character = '0';
encrypted_text = encrypted_text.ToUpper();
char[] alphabet = new char[26] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
Console.WriteLine("The encrypted text is \n{0}", encrypted_text); //Display the encrypted text
for (int i = 0; i < alphabet.Length; i++) //Start a loop which will display 25 different candidates of decipher
{
decoded_text = "";
foreach (char c in encrypted_text)
{
character = c;
if (character == '\'' || character == ' ')
continue;
shift = Array.IndexOf(alphabet, character) - i; //Define a shift which is the index of a character in an alphabet array, take away the itteration of this loop. Store the result in a variable
if (shift <= 0)
shift = shift + 26;
if (shift >= 26)
shift = shift - 26;
decoded_text += alphabet[shift];
}
Console.WriteLine("\nShift {0} \n {1}", i + 1, decoded_text);
}
string filename;
string savePath;
string fileContent = decoded_text;
Console.WriteLine("What do you want to name your file??");
filename = Console.ReadLine();
Console.WriteLine("Where would you like to save your file??");
savePath = Console.ReadLine();
File.WriteAllText(savePath + filename + ".txt", fileContent);
Console.WriteLine("Success");
Console.WriteLine(Console.Read());
}
}// ///////END OF DECRYPTION //////////
有什麼問題嗎? –