特定のユースケースに適合し、コードロジックをもう少し読みやすくするのに役立つ単純なヘルパークラスを使用するのはどうですか.
public class Prompt
{
public struct CursorPosition
{
public int CursorLeft;
public int CursorTop;
}
private CursorPosition _savedPosition;
public Prompt Write(string prompt)
{
Console.Write(prompt);
return this;
}
public Prompt Write(string promptFormat, params object[] args)
{
return Write(string.Format(promptFormat, args));
}
public Prompt WriteLine(string prompt)
{
Write(prompt);
Console.WriteLine();
return this;
}
public Prompt WriteLine(string promptFormat, params object[] args)
{
return WriteLine(string.Format(promptFormat, args));
}
public string ReadLine(bool advanceCursorOnSameLine = false, bool eraseLine = false)
{
if (advanceCursorOnSameLine || eraseLine)
{
SavePosition();
if (eraseLine)
WriteLine(new string(' ', Console.WindowWidth - _savedPosition.CursorLeft)).RestorePosition();
}
var input = Console.ReadLine();
if (advanceCursorOnSameLine)
RestorePosition(input.Length);
return input;
}
public Prompt SavePosition()
{
_savedPosition = GetCursorPosition();
return this;
}
public CursorPosition GetCursorPosition()
{
return new CursorPosition {
CursorLeft = Console.CursorLeft,
CursorTop = Console.CursorTop
};
}
public Prompt RestorePosition(CursorPosition position, int deltaLeft = 0, int deltaTop = 0)
{
var left = Math.Min(Console.BufferWidth - 1, Math.Max(0, position.CursorLeft + deltaLeft));
var right = Math.Min(Console.BufferHeight - 1, Math.Max(0, position.CursorTop + deltaTop));
Console.SetCursorPosition(left, right);
return this;
}
public Prompt RestorePosition(int deltaLeft = 0, int deltaTop = 0)
{
return RestorePosition(_savedPosition, deltaLeft, deltaTop);
}
}
次に、次のように使用できます。
class Program
{
public static void Main(params string[] args)
{
int x, y;
var prompt = new Prompt();
prompt.WriteLine("Please enter the point's coordinates in this form (x,y):");
var savedPos = prompt.GetCursorPosition();
while (true)
{
x = Convert.ToInt32(prompt.Write("(").ReadLine(true, true));
y = Convert.ToInt32(prompt.Write(",").ReadLine(true));
prompt.WriteLine(")");
// do something with x and y
var again = prompt.Write("More (Y):").ReadLine(true, true);
if (!again.StartsWith("Y", StringComparison.OrdinalIgnoreCase))
break;
prompt.RestorePosition(savedPos);
}
}
}