c#中的光标位置

问题描述 投票:0回答:1

我只是一个初学者,如果我的问题是愚蠢的,那就很抱歉。我正在制作程序,询问矩形的大小(AxB)并绘制它。但是我还需要它来询问矩形的位置(X,Y)。有没有办法在c#console app中设置一个比只用于矩形上半部分更长的游标位置?或者简单的事情会让它成为现实吗?这是代码的一部分:

int a;
int b;
int x;
int y;
Console.WriteLine("A");
a = int.Parse(Console.ReadLine());
Console.WriteLine("B");
b = int.Parse(Console.ReadLine());
Console.WriteLine("X");
x = int.Parse(Console.ReadLine());
Console.WriteLine("Y");
y = int.Parse(Console.ReadLine());
Console.Clear();
Console.SetCursorPosition(x, y);
for (int i = 0; i < a; i++)
    Console.Write("*");
Console.Write("\n");
for (int i = 0; i < b - 2; i++)
{
    Console.Write("*");
    for (int k = 0; k < a - 2; k++)
        Console.Write(" ");
    Console.Write("*");
    Console.Write("\n");
}
for (int i = 0; i < a; i++)
    Console.Write("*");
Console.Write("\n");   
c# console position cursor
1个回答
0
投票

到目前为止,这不是最有效或最优雅的解决方案,但它适用于您要做的事情:

        int a;
        int b;
        int x;
        int y;
        Console.WriteLine("A");
        a = int.Parse(Console.ReadLine());
        Console.WriteLine("B");
        b = int.Parse(Console.ReadLine());
        Console.WriteLine("X");
        x = int.Parse(Console.ReadLine());
        Console.WriteLine("Y");
        y = int.Parse(Console.ReadLine());
        Console.Clear();
        Console.SetCursorPosition(x, y);

        for (int i = 0; i < y; i++) //this for will print all the "y" line breaks before your picture prints
        {
            Console.Write("\n");
        }

        for (int j = 0; j < x; j++)
        {
            Console.Write(" ");
        }

        for (int i = 0; i < a; i++)
            Console.Write("*");
        Console.Write("\n");

        for (int i = 0; i < b - 2; i++)
        {
            for (int j = 0; j < x; j++) //This for will print x spaces before each line
            {
                Console.Write(" ");
            }
            Console.Write("*");
            for (int k = 0; k < a - 2; k++)
                Console.Write(" ");
            Console.Write("*");
            Console.Write("\n");
        }
        for (int j = 0; j < x; j++)
        {
            Console.Write(" ");
        }
        for (int i = 0; i < a; i++)
            Console.Write("*");
        Console.Write("\n");

        Console.ReadLine();

只需要在正确的位置打印一些额外的换行符和空格

© www.soinside.com 2019 - 2024. All rights reserved.