单词在 Console.Write() 中消失;

Word disappears in Console.Write();

我的程序显示了字母,然后询问您应该用什么字母替换它:

static void Main(string[] args)
{
    string textToEncode = File.ReadAllText(@"C:\Users\ASUS\Desktop\szyfrowanie2\TextSample.txt");
    textToEncode = textToEncode.ToLower();
    string distinctLetters = new string(textToEncode.Distinct().ToArray());
    var count = textToEncode.Distinct().Count();
    Console.WriteLine("Letters used in text: \n\n");
    int iteration = 0;
    for (int i = 0; i < count; i++)
    {
        if (Equals(distinctLetters[i], ' ')) { Console.Write(" <space> "); }
        else if (Equals(distinctLetters[i], '\r')) { continue; }
        else if (Equals(distinctLetters[i], '\n')) { continue; }
        else { Console.Write(distinctLetters[i] + " "); }
    }
    Console.WriteLine("\n\nEncoding: \nPlease do not use single non-letter characters for coding (such as ',' or '?'");

    List<string> charsUsedToEncode = new List<string>();
    List<string> charsEncoded = new List<string>();
    while (iteration < count)
    {
        if (Equals(distinctLetters[iteration], ' ')) { Console.Write("Swap <space> with "); }
        else { Console.Write("Swap " + distinctLetters[iteration] + " with "); }
        string string1 = Console.ReadLine();
        if (string.IsNullOrEmpty(string1) == true)
        {
            Console.WriteLine("You have to type a character. ");
        }
        else if (string1.Length > 1)
        {
            Console.WriteLine("You entered more than one character. ");
        }
        else
        {
            char SwappingMark = char.Parse(string1);
            if (charsUsedToEncode.Contains(SwappingMark.ToString()))
            {
                Console.WriteLine("\nThis character has already been used.");
            }
            else if (Equals(distinctLetters[iteration], '\r')) { continue; }
            else if (Equals(distinctLetters[iteration], '\n')) { continue; }
            else
            {
                charsEncoded.Add(distinctLetters[iteration].ToString());
                charsUsedToEncode.Add(SwappingMark.ToString());
                SwappingMark = char.ToUpper(SwappingMark);
                textToEncode = textToEncode.Replace(distinctLetters[iteration], SwappingMark);
                iteration++;
            }
        }
    }
    textToEncode = textToEncode.ToLower();
    Console.ReadLine();
}

问题是,在“.”之后char “交换”一词消失。它看起来像这样:

Swap w with a

...

Swap . with x
y with l

“交换”一词消失了。在“.”之后的我的 TextSample 中文本以新行开始,我不知道为什么,因为有一个 else if (Equals(distinctLetters[iteration], '\n')) { continue; } 条件。

正如我在评论中提到的,问题是您在打印之前跳过了换行符,但它们仍在字符串中。因此,您再次跳过它们,但实际上是在向用户打印 "swap" 语句之后(导致这些字符在被忽略之前显示)。

你应该做的不是从一开始就将它们包含在数组中。此外,无需创建新字符串;你可以像这样使用一个字符数组:

char[] distinctLetters = textToEncode.Distinct()
                                     .Where(c=> !"\r\n".Contains(c))
                                     .ToArray();

这样一来,您就不必再次检查 \r\n,因为它们不存在。

完整示例:

static void Main(string[] args)
{
    string textToEncode = File.ReadAllText(@"C:\Users\ASUS\Desktop\szyfrowanie2\TextSample.txt");
    textToEncode = textToEncode.ToLower();
    char[] distinctLetters = textToEncode.Distinct()
                                         .Where(c=> !"\r\n".Contains(c))
                                         .ToArray();
    var count = distinctLetters.Count();
    Console.WriteLine("Letters used in text: \n\n");
    int iteration = 0;
    for (int i = 0; i < count; i++)
    {
        if (distinctLetters[i] == ' ') { Console.Write(" <space> "); }
        else { Console.Write(distinctLetters[i] + " "); }
    }
    Console.WriteLine("\n\nEncoding: \nPlease do not use single non-letter characters for coding (such as ',' or '?'");

    List<string> charsUsedToEncode = new List<string>();
    List<string> charsEncoded = new List<string>();
    while (iteration < count)
    {
        if (distinctLetters[iteration] == ' ') { Console.Write("Swap <space> with "); }
        else { Console.Write("Swap " + distinctLetters[iteration] + " with "); }
        string string1 = Console.ReadLine();
        if (string.IsNullOrEmpty(string1))
        {
            Console.WriteLine("You have to type a character. ");
        }
        else if (string1.Length > 1)
        {
            Console.WriteLine("You entered more than one character. ");
        }
        else
        {
            char SwappingMark = char.Parse(string1);
            if (charsUsedToEncode.Contains(SwappingMark.ToString()))
            {
                Console.WriteLine("\nThis character has already been used.");
            }
            else
            {
                charsEncoded.Add(distinctLetters[iteration].ToString());
                charsUsedToEncode.Add(SwappingMark.ToString());
                SwappingMark = char.ToUpper(SwappingMark);
                textToEncode = textToEncode.Replace(distinctLetters[iteration], SwappingMark);
                iteration++;
            }
        }
    }
    textToEncode = textToEncode.ToLower();
    Console.ReadLine();
}