在 C# 控制台中自动修改变量



我正在尝试自动修改控制台中的变量,而无需修改其余文本。 为此,我已经尝试制作一个无限循环,擦除控制台并注意到所有行,但它会产生流动性问题,因此我正在寻找一种仅修改变量而不接触控制台其余部分的方法。

int total = 10000;
int remaining = remaining - total;
Console.WriteLine("Remaining : " + remaining"); <---- Here I want to modify the remaining variable without having to clear the console

您可以在所述位置覆盖控制台缓冲区:

using System;
var numberVariable = 1;
var text = "Hello World Times " + numberVariable;
while (true)
{
Thread.Sleep(500);
//writes the text into 0|0
Console.WriteLine(text);
//LOOPBEGIN
//The cursor is now on 0|1
//Console.WriteLine advances the cursor to the next line so we need to set it to the previous line
//Ether by:
//Console.SetCursorPosition(0 /**Column 0**/, Console.CursorTop - 1);
//or by
Console.CursorTop = Console.CursorTop - 1;
Console.CursorLeft = 0;
//The cursor is now again on 0|0
numberVariable = numberVariable + 1;
//increment the variable
text = "Hello World Times " + numberVariable;
//ether set the text variable again or use a new variable
}

https://learn.microsoft.com/de-de/dotnet/api/system.console.setcursorposition?view=netframework-4.8

最新更新