在C#中的控制台上打印ASCII字符

问题描述 投票:1回答:2

我需要让控制台打印ASCII char,每个“页面”有22个字符。使用输入,比如说“key”,它们将打印接下来的22个ASCII字符,依此类推。问题在于“翻页”问题。

这是我的代码:

    static void Main(string[] args)
    {

        int i = 0;

        while (i <= 22)
        {
            Console.Write(i + " = " + (char)i);

            if (i < 22)
            {
                Console.Write((char)10);
            }

            i++;
        }

        Console.Write("Please press any key to turn page");

        Console.ReadKey();

        while (i > 22 && i <= 44)
        {
            Console.Write(i + " = " + (char)i);

            if (i < 44)
            {
                Console.Write((char)10);
            }

            i++;
        }

        Console.Write("Please press any key to turn page");

        Console.ReadKey();
    }

我本质上是一个新手。我自己学习大部分的东西,所以如果我在学术上难以忍受,请耐心地告诉我它是如何完成的。我可以从那里过来。提前致谢。

c# console ascii
2个回答
3
投票

据我所知,你试图用22个字符的部分打印所有ASCII表。

这基本上可以通过以下代码片段来完成:

for (int i = 1; i < 256; i++)
{
    Console.WriteLine(i + " = " + (char)i);

    if (i % 22 == 0)
    {
        Console.WriteLine("Please press any key to turn page");
        Console.ReadKey();
        Console.Clear();
    }
}

在这里,我们在ASCII表中迭代所有255个字符,逐行编写它们。

打印出每个字符后,我们检查它是否是第22个字符计数(qazxsw poi表示“从i分成22的余数” - 因此在22,44,66等时它将为0)。

如果它是22,44,66等字符 - 我们打印“按任意键”,读取输入然后清除屏幕。

而已。


2
投票

如果您不使用i % 22控制台将为某些ASCII字符提供不同的符号或不正确的类型面。有关更多信息System.Text.Encoding.GetEncoding(28591);

GetEncoding(28591)

/*internal const int ISO_8859_1  = 28591;// Latin1;*/

using System; namespace AsciiChart { class Program { static void Main(string[] args) { Console.OutputEncoding = System.Text.Encoding.GetEncoding(28591); for (int i = 0; i < 256; i++) { Console.Write(i+"=> ["+(char)i +"] \n"); } Console.ReadKey(); } } }

编辑:为了更好的格式我用这个编辑了源代码。

enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.