如何使输出成为连续字符串,而不是堆叠在每个字母 - c# 的顶部

本文关键字:顶部 输出 何使 连续 字符串 | 更新日期: 2023-09-27 18:26:44

public class LetterArray 
{
    internal static string[] Alphabet() 
    {
        var letterValues = new string[26];
        letterValues[0] = "A";
        letterValues[1] = "B";
        letterValues[2] = "C";
        letterValues[3] = "D";
        letterValues[4] = "E";
        letterValues[5] = "F";
        letterValues[6] = "G";
        letterValues[7] = "H";
        letterValues[8] = "I";
        letterValues[9] = "J";
        letterValues[10] = "K";
        letterValues[11] = "L";
        letterValues[12] = "M";
        letterValues[13] = "N";
        letterValues[14] = "O";
        letterValues[15] = "P";
        letterValues[16] = "Q";
        letterValues[17] = "R";
        letterValues[18] = "S";
        letterValues[19] = "T";
        letterValues[20] = "U";
        letterValues[21] = "V";
        letterValues[22] = "W";
        letterValues[23] = "X";
        letterValues[24] = "Y";
        letterValues[25] = "Z";
        return letterValues;
    }
}

public class decrypt
{
    public static void Main() //Main method
    {
        int res = 34;
        string[] letterValues = LetterArray.Alphabet();
        //Create for loop that runs through every possible shift value
        for (int shift = 0; shift <= 25; shift++) 
        {
            Console.WriteLine("'nShift Value = " + shift + ": ");
            // For each character in the text file
            foreach (var ch in ReadText.cipherTxt()) {
                if (ch == ' ') 
                { } 
                else 
                {
                    for (int i = 0; i <= 25; i++) 
                    {
                        if ((ch.ToString().ToUpper()) == letterValues[i]) 
                        {
                            res = i;
                        }
                    }
                    if (shift > res) 
                    {
                        Console.WriteLine(letterValues[26 - (shift - res)][0]);
                    } 
                    else 
                    {
                        Console.WriteLine(letterValues[res - shift][0]);
                    }
                }
            }                    
        }
    }
}

不确定如何输出以下内容,因此要成为连续字符串,而不是每个字母堆叠在一起。我试图更改Console.WriteLine值,但它似乎弄乱了程序并抛出错误。

如何使输出成为连续字符串,而不是堆叠在每个字母 - c# 的顶部

如果您不希望每个值都位于新行中,请使用Console.Write

Console.Write(letterValues[res - shift]);

您还可以使用StringBuilder并为其Append字符。然后在循环后打印一次。