简体   繁体   中英

Why “write” function doesn't work at first time?

At result write function doesn't work for "8", but it writes 7,6,5,... respectively. when I trace code , I can't find anything wrong...(code must draw a chess board with numbers)

Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("   a   b   c   d   e   f   g   h");
        Console.BackgroundColor = ConsoleColor.Blue;
        Console.Write(" ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗\n");
        Console.ForegroundColor = ConsoleColor.Blue;
        for (int i = 7; i > -1;i--)
        {
            Console.Write((i + 1).ToString());//Problem is here
            for (int j=0;j<8;j++)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.BackgroundColor = ConsoleColor.Blue;
                Console.Write("║");
                if(i%2!=0)
                {
                    if(j%2==0)
                    {
                        Console.BackgroundColor = ConsoleColor.White;
                    }
                    else
                    {
                        Console.BackgroundColor = ConsoleColor.Black;
                    }
                }
                else
                {
                    if (j % 2 == 0)
                    {
                        Console.BackgroundColor = ConsoleColor.Black;
                    }
                    else
                    {
                        Console.BackgroundColor = ConsoleColor.White;
                    }
                }
                Console.Write(" "+ch[i,j].ToString()+" ");
            }
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.Write("║\n");
            if(i!=0)
            {
                Console.WriteLine(" ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣");
            }
            else
            {
                Console.WriteLine(" ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝");
            }
        }

It is working, but you cannot see it.

Try

Console.Write(" ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗\n");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Blue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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