简体   繁体   English

for循环c#的格式化问题

[英]Formatting Issue with for loop c#

I'm trying to create a game called 'NIM' (See code introduction if you aren't familiar).我正在尝试创建一个名为“NIM”的游戏(如果您不熟悉,请参阅代码介绍)。 When I output the 'blocks' they aren't spaced out evenly.当我 output “块”时,它们的间距不均匀。 I may be missing the obvious, but can someone point out where I'm going wrong.我可能错过了显而易见的事情,但有人可以指出我哪里出错了。

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

The code below is your code with some minor modifications.下面的代码是您的代码,稍作修改。 \t has been removed and replaced with spaces. \t已被删除并替换为空格。 Spaces added before writing the column headers.在写入列标题之前添加空格。 Added comments.添加了评论。

Try the following:尝试以下操作:

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

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

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