提问者:小点点

用for循环c#格式化问题


我正在尝试创建一个名为“NIM”的游戏(如果您不熟悉,请参阅代码介绍)。当我输出“块”时,它们的间隔不是均匀的。我可能错过了显而易见的,但有人能指出我错在哪里吗。

using System;
using System.Threading;

namespace NIM
{
    class Program
    {
        static void Main(string[] args)
        {
            Introduction();
            InitialBoardSetUp();
        }
        static void Introduction()
        {
            Console.WriteLine("\t\t\t\tWelcome to NIM!\n");
            Console.WriteLine("   - Each player takes their turn to remove a certain number of 'blocks' from a stack, of which there are 7.");
            Console.WriteLine("   - This happens until there is only 1 'block' remaining. With the winner being the one to remove the last 'block'.\n");
            Console.WriteLine("Initialising the board:\n");
            Thread.Sleep(2000);
        }
        static void InitialBoardSetUp()
        {
            for (int i = 1; i <= 7; i++)
            {
                Console.Write("     " + i + "\t");
            }
            
            Console.Write("\n\n");

            for (int i = 1; i <= 7; i++)
            {
                Console.Write(" "+ i);

                for (int j = 1; j <= 7; j++)
                {
                    Console.Write("  ███\t");
                }
                Console.Write("\n");
            }
        }
    }
}

共1个答案

匿名用户

下面的代码是经过一些小修改的代码。\t已被删除并替换为空格。在写入列标题之前添加的空格。添加评论。

请尝试以下操作:

using System;
using System.Threading;

namespace NIM
{
    class Program
    {
        static void Main(string[] args)
        {
            Introduction();
            InitialBoardSetUp();
        }

        static void Introduction()
        {
            Console.WriteLine("\t\t\t\tWelcome to NIM!\n");
            Console.WriteLine("   - Each player takes their turn to remove a certain number of 'blocks' from a stack, of which there are 7.");
            Console.WriteLine("   - This happens until there is only 1 'block' remaining. With the winner being the one to remove the last 'block'.\n");
            Console.WriteLine("Initialising the board:\n");
            //Thread.Sleep(2000);
        }

        static void InitialBoardSetUp()
        {
            //add space for row numbers
            Console.Write("  ");

            //print column headers
            for (int i = 1; i <= 7; i++)
            {
                Console.Write("   " + i + "   ");
            }

            Console.Write("\n\n");

            for (int i = 1; i <= 7; i++)
            {
                //print row numbers
                Console.Write(" " + i);

                for (int j = 1; j <= 7; j++)
                {
                    //print blocks
                    Console.Write("  ███  ");
                }

                Console.Write("\n");
            }
        }
    }
}