簡體   English   中英

for循環c#的格式化問題

[英]Formatting Issue with for loop c#

我正在嘗試創建一個名為“NIM”的游戲(如果您不熟悉,請參閱代碼介紹)。 當我 output “塊”時,它們的間距不均勻。 我可能錯過了顯而易見的事情,但有人可以指出我哪里出錯了。

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

下面的代碼是您的代碼,稍作修改。 \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");
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM