简体   繁体   中英

Formatting Issue with for loop c#

I'm trying to create a game called 'NIM' (See code introduction if you aren't familiar). When I output the 'blocks' they aren't spaced out evenly. 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. 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");
            }
        }
    }
}

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