繁体   English   中英

在C#中的光标位置

[英]Cursor position in c#

我只是一个初学者,如果我的问题很愚蠢,请对不起。 我正在编写程序,询问矩形的大小(AxB)并将其绘制。 但是我还需要询问矩形的位置(X,Y)。 有什么方法可以在c#控制台应用程序中设置一个比仅在矩形的上部工作更长的光标位置? 还是简单的事情就能实现? 这是代码的一部分:

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

到目前为止,这不是最有效或最优雅的解决方案,但它将满足您的需求:

        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();

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

暂无
暂无

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

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