Created
June 2, 2021 02:03
-
-
Save Ruffo324/372adfc74a1b35e5b0cda212aa9d8b12 to your computer and use it in GitHub Desktop.
Verwendet einen bereits beschriebenen Teil der Konsole wieder.. Dient eher als Beispielcode.. Der Nachteil der Lösung ist, das dass gelöschte überschrieben werden muss mit spaces ' '.. Der "richtigere" Weg würde über die manipulation des Buffers stattfinden..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#nullable enable | |
using System; | |
using System.Reactive.Disposables; | |
using System.Threading.Tasks; | |
namespace CoreCon | |
{ | |
internal class Program | |
{ | |
public static async Task Main() | |
{ | |
Console.WriteLine("Gib eine ZAHL ein: "); | |
Console.Write("DEINE EINGABE: "); | |
var cursorPos = (Console.CursorLeft, Console.CursorTop); // ⚠️ merken der Position. | |
int versuche; | |
int zahl; | |
for (versuche = 0; !int.TryParse(Console.ReadLine(), out zahl); versuche++) // Hier wird die eingabe u.a. eingelesen.. | |
{ | |
await WrongInput(); | |
// ⚠️ Hier also Position erstmal wieder auf die oben gemerkte.. | |
Console.SetCursorPosition(cursorPos.CursorLeft, cursorPos.CursorTop); | |
Console.Write(string.Empty.PadRight(Console.WindowWidth - cursorPos.CursorLeft)); // Wieder mit " " leerne.. | |
Console.SetCursorPosition(cursorPos.CursorLeft, cursorPos.CursorTop); // wieder an die position.. | |
} | |
Console.WriteLine($"WOW.. Die Zahl '{zahl}'. Krass D:"); | |
Console.WriteLine($"Hast ja auch nur '{versuche}' Versuche gebraucht.."); | |
Console.ReadKey(); // Programm offen halten.. | |
} | |
private static async Task WrongInput() | |
{ | |
// Der User hat keine natürliche Zahl eingegeben: | |
using (Disposable.Create(Console.ForegroundColor, currCol => Console.ForegroundColor = currCol)) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
await WriteWaitClean("DAS war keine Zahl!"); | |
await WriteWaitClean("NOCHMAL!"); | |
} | |
async Task WriteWaitClean(string oneLineText) | |
{ | |
Console.Write(oneLineText); | |
await Task.Delay(TimeSpan.FromSeconds(1)); | |
Console.SetCursorPosition(0, Console.CursorTop); // ⚠️ Hier wollen wir nur zurück zum beginn der Zeile.. also Left 0., Top = Top | |
Console.Write(string.Empty.PadRight(Console.WindowWidth)); // Einfach eine Zeile voll mit Leerzeichen schreiben.. | |
Console.SetCursorPosition(0, Console.CursorTop); // ⚠️ Hier wollen wir nur zurück zum beginn der Zeile.. also Left 0., Top = Top | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment