簡體   English   中英

如何在C#控制台應用程序中創建空心方塊(用戶參數)?

[英]How to create a hollow square (user parameters) in C# console applications?

我是初學者,在初級Pascal方面有一點經驗。 我目前正在試圖弄清楚如何在C#中創建一個帶有用戶定義參數的空心方塊。 我已經成功地從4個方面中獲得了3個但我對如何管理第4個方面的想法不滿意。 到目前為止這是我的代碼:

class Program
{
    static void Main(string[] args)
    {
        int height;
        int width;
        int counterH = 0;
        int counterW2 = 0;
        int counterW1 = 0;
        Console.WriteLine("Please input the sizes of the square!");
        Console.WriteLine("Please input the height of the square.");
        height = int.Parse(Console.ReadLine());
        Console.WriteLine("Please input the width of the square");
        width = int.Parse(Console.ReadLine());
        while (counterW1 < width)
        {
            Console.Write("--");
            counterW1++;}
        Console.WriteLine();
            while (counterH < height)
            {
                Console.WriteLine("|");
                counterH++;
            }
            while (counterW2 < width)
            {
                Console.Write("--");
                counterW2++;
            }
            Console.ReadLine();
        }
    }

如果你認為我的不好,你會建議一個更簡單/更好/更優化的解決方案,我也很高興。 非常感謝您的寶貴時間!

在你的while (counterH < height)循環中,你應該遍歷你的width-Value並放置一些空格。 在末端,您可以放置​​煙斗| 再次。

在此位置使用Console.Write()而不是Console.WriteLine() ,因為您不希望在第一個Pipe之后使用LineFeed / CarriageReturn。

例:

    while (counterH < height)
    {
        Console.Write("|");
        int counterW3 = 0;
        while (counterW3 < width)
        {
            Console.Write(" ");
            counterW3++;
        }
        Console.Write("|" + System.Environment.NewLine);
        counterH++;
    }

暫無
暫無

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

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