gpt4 book ai didi

c# - 将控制台分成两部分以获得两个输出

转载 作者:行者123 更新时间:2023-11-30 14:19:03 25 4
gpt4 key购买 nike

我正在创建一个控制台应用程序,我希望其中有两个输出和一个输入。原因是一个输出始终可见。

This is the first output
Text flows upwards just like a regular console application, however...

---------
This is a second output
This is placed at the bottom of the console // also input goes here.

我想这样调用它

 Console.Out.Writeline("This is the first output");
Console.Out.Writeline("Text flows upwards just like a regular console application, however...");
MyTextWriter.WriteLine("This is a second output");
MyTextWriter.WriteLine("This is placed at the bottom of the console");

但是我该如何将控制台分成两部分呢?有可能吗?

最佳答案

如果我的理解正确,您可以使用 Console.SetCursorPosition 在您想要的位置绘制文本。这是一个粗略的示例,它将控制台分成两个区域,添加的文本向上流动。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static List<string> area1 = new List<string>();
static List<string> area2 = new List<string>();
static int areaHeights = 0;

static void Main(string[] args)
{
// Number of rows for each area
areaHeights = (Console.WindowHeight - 2) / 2;

drawScreen();

int i = 0;
while (true)
{
i++;

// jumb between areas
if (i % 2 == 0)
AddLineToBuffer(ref area1, Console.ReadLine());
else
AddLineToBuffer(ref area2, Console.ReadLine());

drawScreen();
}
}

private static void AddLineToBuffer(ref List<string> areaBuffer, string line)
{
areaBuffer.Insert(0, line);

if (areaBuffer.Count == areaHeights)
{
areaBuffer.RemoveAt(areaHeights - 1);
}
}


private static void drawScreen()
{
Console.Clear();

// Draw the area divider
for (int i = 0; i < Console.BufferWidth; i++)
{
Console.SetCursorPosition(i, areaHeights);
Console.Write('=');
}

int currentLine = areaHeights - 1;

for (int i = 0; i < area1.Count; i++)
{
Console.SetCursorPosition(0, currentLine - (i + 1));
Console.WriteLine(area1[i]);

}

currentLine = (areaHeights * 2);
for(int i = 0; i < area2.Count; i++)
{
Console.SetCursorPosition(0, currentLine - (i + 1));
Console.WriteLine(area2[i]);
}

Console.SetCursorPosition(0, Console.WindowHeight - 1);
Console.Write("> ");

}

}
}

我假设您不仅要用用户输入来填充每个区域?如果是这样,您需要在单独的线程中设置控制台的绘图,并让该线程在需要时更新屏幕。

关于c# - 将控制台分成两部分以获得两个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3434346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com