简体   繁体   English

字在 Console.Write() 中消失;

[英]Word disappears in Console.Write();

My program shows the letter and then asks with what letter should you replace it:我的程序显示了这封信,然后询问您应该用什么字母替换它:

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();
}

The problem is, after a '.'问题是,在“。”之后char The word "Swap" disappears. char “交换”这个词消失了。 It looks like this:它看起来像这样:

Swap w with a用 a 交换 w

... ...

Swap.交换。 with x与 x
y with l y 与 l

The word "Swap" disappears. “交换”这个词消失了。 In my TextSample after a '.'在我的 TextSample 中,在“。”之后text starts in new line and I don't know why, since there is a else if (Equals(distinctLetters[iteration], '\n')) { continue; }文本从新行开始,我不知道为什么,因为有一个else if (Equals(distinctLetters[iteration], '\n')) { continue; } else if (Equals(distinctLetters[iteration], '\n')) { continue; } condition. else if (Equals(distinctLetters[iteration], '\n')) { continue; }条件。

As I mentioned in the comments, the problem is that you skip the new-line characters before printing them but they're still in the string.正如我在评论中提到的,问题是您在打印换行符之前跳过了它们,但它们仍在字符串中。 So, you skip them again but after actually printing the "swap" statement to the user (causing those characters to be displayed before they're ignored).因此,您再次跳过它们,但在实际向用户打印“交换”语句之后(导致这些字符在被忽略之前显示)。

What you should do instead is not include them in the array from the beginning.相反,您应该做的不是从一开始就将它们包含在数组中。 Also, there's no need to create a new string;此外,无需创建新字符串; you can just use a char array like this:您可以像这样使用 char 数组:

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

That way, you never have to check for \r or \n again since they don't exist.这样,您就不必再次检查\r\n了,因为它们不存在。

Full example:完整示例:

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM