在 C# 控制台中自动修改变量
Modify a variable automatically in the C# console
我正在尝试在不修改其余文本的情况下自动修改控制台中的变量。
为此,我已经尝试创建一个无限循环来擦除控制台并注意到所有行,但它会产生流动性问题,因此我正在寻找一种仅修改变量而不触及控制台其余部分的方法。
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://docs.microsoft.com/de-de/dotnet/api/system.console.setcursorposition?view=netframework-4.8
我正在尝试在不修改其余文本的情况下自动修改控制台中的变量。 为此,我已经尝试创建一个无限循环来擦除控制台并注意到所有行,但它会产生流动性问题,因此我正在寻找一种仅修改变量而不触及控制台其余部分的方法。
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://docs.microsoft.com/de-de/dotnet/api/system.console.setcursorposition?view=netframework-4.8