C# :: Aufgabe #1
69 Lösungen

Erfolgreiche Eingabe einer Integer - Zahl
Anfänger - C#
von Gustl
- 27.07.2012 um 15:08 Uhr
Erstelle eine Konsolenanwendung in der Benutzer so lange aufgefordert wird, eine ganze Integer - Zahl einzugeben, bis dies erfolgreich war.
Lösungen:
Mein Vorschlag für diese Aufgabe:
C#-Code

using System; namespace EingabeZahl { class Program { static void Main(string[] args) { //Bitte den user eine Zahl einzugeben. Console.WriteLine("Bitte geben sie eine Zahl ein"); //Variable die besagt, ob die Sclheifen durchlaufen werden soll. bool dummy2 = true; //Starte Schleife, die solang läuft bis eine Zahl eingegeben wird. while (dummy2 == true) { //Warte auf eingabe, und übergebe diese an den string "dummy". string dummy = Console.ReadLine(); int dummy1 = 0; //Überprüfe ob dummy "leer" ist. if(dummy != "") { //Überprüfe, ob der String "dummy" eine Zahl ist. if(Int32.TryParse(dummy, out dummy1)) { //Ausgabe das die eingabe eine zahl ist + eingegebende Zahl. Console.WriteLine("Die eingegebende EingabeZahl ist: " + dummy); //Bricht die Schleife ab. dummy2 = false; } else { //Ausgabe das die eingabe keine zahl ist. Console.WriteLine("Bitte geben sie eine Zahl ein"); } } else { //Lässt das programm eine Secunde warten. Console.WriteLine("Bitte geben sie eine Zahl ein"); System.Threading.Thread.Sleep(500); } } Console.WriteLine("Belibige Taste drücken, um das Programm zu beenden..."); //Warte auf eingabe, befor das Programm beendet wird. Console.ReadKey(); } } }

using System; namespace TYP_CSharp1 { internal class Program { private static void Main(string[] args) { bool isValid = false; while (!isValid) { Console.WriteLine("Geben sie eine ganze Integer Zahl ein:"); string input = Console.ReadLine(); int number; isValid = int.TryParse(input, out number); } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int zahl =0; bool result = false; string eingabe; while (result == false) { Console.Write("Bitte eine Ganzzahl eingeben: "); eingabe = Console.ReadLine(); result = Int32.TryParse(eingabe, out zahl); if (result) { Console.WriteLine(zahl + " ist eine Ganzzahl"); } else { Console.WriteLine("Fehler bei der Eingabe\nDies war keine Ganzzahl\n"); } } Console.WriteLine("Zum Beenden eine Beliebige Taste druecken..."); Console.ReadKey(); } } }

using System; namespace TrainYourProgrammer { public class Program { static void Main(string[] args) { bool inputIsValid = false; int value = default(int); while (!inputIsValid) { Console.Write("Gebe ganze Integer-Zahl ein: "); string userInput = Console.ReadLine(); if (int.TryParse(userInput, out value)) { inputIsValid = true; } } } } }
Kurz und knackig...
C#-Code

namespace TrainYourProgrammer { public class Program { public static void Main(string[] args) { Int32 value; do { Console.WriteLine("Please enter a valid Int32 value: "); } while ( !Int32.TryParse( Console.ReadLine(), out value ) ); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication44 { class Program { static void Main(string[] args) { bool success = false; do{ Console.WriteLine("Geben sie eine Integerzahl ein!"); string input = Console.ReadLine(); int result; if(Int32.TryParse(input, out result)) { Console.WriteLine("Danke!"); success = false; } else { Console.WriteLine("Keine gültige Eingabe!"); success = true; } }while(success); Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IntegerzahlEingeben__Nr._1_ { class Program { static void Main(string[] args) { bool termination = false; while (termination == false) { Console.WriteLine("Geben Sie bitte eine ganze Zahl ein."); int number; string line = Console.ReadLine(); if (int.TryParse(line, out number)) { termination = true; Console.WriteLine("Diese Zahl ist eine gültige Integer-Zahl."); } } Console.ReadKey(); } } }

using System; namespace Eingabe { class Program { static void Main(string[] args){ string i; Console.WriteLine("Bitte geben Sie eine ganze Zahl ein!"); i= Console.ReadLine(); Boolean mybool = false; while (!mybool){ try{ int x = Convert.ToInt32(i); mybool = true; Console.WriteLine("Richtig!"); } catch (Exception e){ Console.WriteLine(e.Message); Console.WriteLine("Bitte geben Sie eine ganze Zahl ein!"); i = Console.ReadLine(); } } Console.Read(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace lesson1_Eingabe_Integer { class Program { static void Main(string[] args) { string eingabe; int ausgabe; do { Console.WriteLine("Integer eingeben: "); eingabe = Console.ReadLine(); if (Int32.TryParse(eingabe, out ausgabe)) { Console.WriteLine("Korrekt, Ausgabe: {0}", ausgabe); } else { Console.WriteLine("Falscher Datentyp, noch mal!"); } } while (ausgabe == 0); Console.ReadLine(); } } }

using System; namespace IntegerEingabe { class Program { static void Main(string[] args) { Console.Write("Geben Sie einen Integer ein: "); Eingabe(); } public static void Eingabe() { try { Convert.ToInt32(Console.ReadLine()); } catch { Console.Write("Geben Sie einen Integer ein: "); Eingabe(); } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IntegerCheck { class Program { static void Main(string[] args) { int temp; Console.WriteLine("Bitte geben Sie eine Integer Zahl ein:"); while (!int.TryParse(Console.ReadLine(), out temp)) { Console.WriteLine("Bitte geben Sie eine Integer Zahl ein:"); } } } }
Eine Lösung welche den Overload von int nicht beachtet ginge wie folgt:
C#-Code

static void Main(string[] args) { do { Console.WriteLine("Bitte geben Sie eine Integer Zahl ein:"); } while (!(Console.ReadLine().All(char.IsDigit))); }

using System; namespace TYP_01 { class Program { static void Main(string[] args) { int result = 0; string value = ""; while (true) { Console.WriteLine("Bitte geben sie eine ganze Zahl ein!"); value = Console.ReadLine(); try { result = Convert.ToInt32(value); break; } catch (OverflowException) { } catch (FormatException) { } } Console.ReadLine(); } } }

class IntegerNumber { static void Main(string[] args) { int result; bool isInteger = false; while (!isInteger) { Console.WriteLine("Geben Sie bitte eine ganze Integer-Zahl ein: "); string input = Console.ReadLine(); isInteger = Int32.TryParse(input, out result); if (isInteger) { Console.Write(result + " ist eine ganze Integer-Zahl."); Console.ReadKey(); } } } }

public static int intEingabe() { int temp; do { Console.WriteLine("Bitte geben Sie eine gültige Integer Zahl ein!"); } while (!Int32.TryParse(Console.ReadLine(), out temp)); return temp; }

using System; using System.Threading; class Program { static void Main(string[] args) { Console.WriteLine("Hallo aus Russland!"); Console.WriteLine("Bitte eingeben Sie Zahl fur Eingang in Programm: "); string result = Console.ReadLine(); bool prov = false; int zahl = 0; while (!prov) { prov = Int32.TryParse(result, out zahl); if (prov) { Console.WriteLine("Herzliche Glückwünsche! Sie haben erfolgreich in Programm eingegeben!"); break; } else { Console.WriteLine("Sie haben falschen Bedeutung eingegeben! Man eingebem nur Zahlen!"); Console.WriteLine("Bitte eingeben Sie Zahl fur Eingang in Programm!"); result = Console.ReadLine(); } } Thread.Sleep(5000); } }

using System; namespace ConsoleApplication11 { internal class Program { private static void Main(string[] args) { Console.WriteLine("Geben sie eine ganze Integer Zahl ein:"); int ganzezahl; bool richtig = false; while (!richtig) { var eingabe = Console.ReadLine(); if (int.TryParse(eingabe, out ganzezahl)) { Console.WriteLine("Ok"); richtig = true; } else { Console.WriteLine("Bitte nur ganze Integer Zahl eingeben"); } } } } }

/* * Erstellt mit SharpDevelop. * Benutzer: especk * Datum: 27.01.2015 * Zeit: 16:34 * * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern. */ using System; namespace Erfolgreiche_Eingabe_einer_Integer_Zahl { class Program { public static void Main(string[] args) { int zahl; start:{ Console.WriteLine("Bitte ganze Integer-Zahl eingeben.");} try { zahl = int.Parse(Console.ReadLine()); } catch (Exception) { goto start; } if(zahl<0){goto start;} Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Aufgabe_1 { class Program { static void Main(string[] args) { string userInput ="leer"; while (!checkInput(userInput)) { Console.WriteLine("Bitte geben Sie eine ganze Zahl ein!!"); userInput = Console.ReadLine(); } Console.ReadLine(); } public static Boolean checkInput(string input) { int result; bool erg; if (! string.IsNullOrEmpty(input) && Int32.TryParse(input,out result )) { Console.WriteLine("Ihre Eingabe war korrekt!"); erg = true; } else if(input =="leer") { erg = false; } else { Console.WriteLine("Ihre EIngabe war keine ganze Zahl!"); Console.WriteLine(); erg = false; } return erg; } } }

class Program { static void Main(string[] args) { int anInteger; Console.Write("Give me an Integer: "); while (!Int32.TryParse(Console.ReadLine(), out anInteger)) { Console.Write("Try again: "); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Übungen { class Program { static void Main(string[] args) { int y=0; bool result=false; while(result==false){ Console.WriteLine("Bitte gebe eine Zahl ein"); string zahl = Console.ReadLine(); result =Int32.TryParse(zahl, out y); if(result){ Console.WriteLine(y+" ist eine Ganzezahl"); } else{ Console.WriteLine("fehler bei der Eingabe, dies war keine Ganzezahl"); } } Console.WriteLine("Zum Beenden eine Beliebige Taste drücken..."); Console.ReadKey(); } } }

public static void isNumber() { while (true) { string Input = Console.ReadLine(); try { Int32.Parse(Input); break; } catch { } } }
Solution using regex.
C#-Code

using System; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace Uebung1 { class Program { static void Main(string[] args) { Console.WriteLine("Typ a number:"); Regex rg = new Regex("^[0-9]+$"); while (true) { Match match = rg.Match(Console.ReadLine()); if (match.Success) break; else Console.WriteLine("Try again."); } Console.WriteLine("Correct input"); Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Integer { class Program { static void Main(string[] args) { bool checkInt= false; while (!checkInt) { Console.WriteLine("Bitte eine Integer-Zahl eingeben:"); string checknumber = Console.ReadLine(); int number; checkInt = int.TryParse(checknumber, out number); } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IntEingabe { class Program { static void Main(string[] args) { bool IsInt = false; int zahl; do { Console.Write("Geben Sie eine Zahl ein: "); IsInt = int.TryParse(Console.ReadLine(), out zahl); Console.Clear(); }while(!IsInt); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace csharp_wiederholteeingabe { class Program { static void Main(string[] args) { int x; do { Console.WriteLine("Bitte geben Sie eine Ganzzahl ein:"); } while (!int.TryParse(Console.ReadLine(), out x)); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Uebung1 { class Program { static void Main(string[] args) { int eingabe = 0; bool done = false; do { Console.Clear(); Console.WriteLine("Geben sie ein ganzzahlige Zahl zwischen {0} und {1} ein.", Int32.MinValue, Int32.MaxValue); try { eingabe = Convert.ToInt32(Console.ReadLine()); } catch (System.FormatException e) { done = false; Console.Clear(); Console.WriteLine("Unzulässiger Integer-Wert."); Console.WriteLine(e.ToString() + "\n\n\nDrücke Enter..."); Console.ReadLine(); continue; } done = true; } while (done != true); Console.WriteLine("Ihre eingegebene Zahl ist {0}.", eingabe); Console.ReadLine(); } } }
Mein Versuch :)
C#-Code

using System; namespace Erfolgreiche_Eingabe_einer_Integer___Zahl { class Program { static void Main(string[] args) { string input; int number; bool error = true; do { Console.Write("Bitte geben sie eine Int-Zahl ein: "); input = Console.ReadLine(); try { number = Convert.ToInt32(input); error = false; Console.WriteLine("Sie haben die Zahl {0} eingegeben.", number); } catch (Exception) { Console.WriteLine("Sie haben keine Int-Zahl eingegeben"); } } while (error); Console.Write("Zum Beenden Enter drücken..."); Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Aufg01 { class Program { static void Main(string[] args) { bool isInt = false; int compare; while (!isInt) { isInt = int.TryParse(Console.ReadLine(), out compare); if (isInt) Console.WriteLine("Die Eingabe entspricht eine Integer Ganzzahl! Das Programm wird beendet!"); else Console.WriteLine("Die Eingabe entspricht keiner Integer Ganzzahl!"); } } } }

Console.WriteLine("Please enter an Integer"); string x = Console.ReadLine(); int value; if (int.TryParse(x, out value)) { Console.WriteLine("You entered a valid Integer: " + x); } else { Console.WriteLine("Not a valid Integer"); } Console.ReadLine();

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Uebungen { class Program { static void Main(string[] args) { bool isInteger = false; while (!isInteger) { Console.Write("Geben Sie eine ganze Zahl ein: "); string input = Console.ReadLine(); int result; isInteger = int.TryParse(input, out result); if (isInteger) { Console.WriteLine(result + " ist ein Integer."); } else { Console.WriteLine("Ihre Eingabe war falsch!"); } } Console.ReadKey(); } } }

namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int eingabe = 0; string eingabeS = null; while(true) { Console.Out.WriteLine("Bitte geben Sie eine Ganzzahl ein: "); eingabeS = Console.In.ReadLine(); try { eingabe = Convert.ToInt32(eingabeS); Console.Out.WriteLine("Die eingegebene Zahl '" + eingabe + "' ist eine Ganzzahl." +"Das Programm wird nun beendet."); Console.In.ReadLine(); break; } catch { Console.Out.WriteLine("Sie haben keine Ganzzahl eingegeben."); Console.In.ReadLine(); } } } } }

using System; namespace TrainYourProgrammer { class Program { static void Main(string[] args) { int number; bool isInt = false; string input; while (!isInt) { Console.WriteLine("Bitte geben Sie eine Integer-Zahl ein: "); input = Console.ReadLine(); if (Int32.TryParse(input, out number)) { isInt = true; Console.WriteLine("Integer-Zahl erkannt: " + number); } else { Console.WriteLine("Unerwartete Eingabe. \'" + input + "\' ist keine Integer-Zahl."); } Console.WriteLine(); } Console.WriteLine("Drücken Sie eine beliebige Tasten zum Beenden."); Console.ReadKey(); } } }
kurz und unkompliziert
C#-Code

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Boolean isValid = false; int num; do { Console.WriteLine("Enter an Integer: "); isValid = int.TryParse(Console.ReadLine(), out num); } while (!isValid); } } }

using System; namespace PrivateÜbungKonsole { class Program { static void Main(string[] args) { // in i wird später der Wert eingelesen int i = 1; // Konsole fordert dazu auf int Zahl einzugeben Console.Write("Geben Sie eine int Zahl ein\n"); // solange i nicht 0 ist soll das Programm wiederholt werden while (i != 0) { // Das hier sieht finde ich einfach besser aus hat aber keinen Zweck Console.Write(">> "); // Es wird ein try/catch-Block eingeleitet für mögliche Fehler try { // Wenn alles glatt läuft wird i der Wert zugewiesen und auf 0 gestellt um die Schleife zu unterbrechen i = Convert.ToInt32(Console.ReadLine()); i = 0; } catch (Exception ex) { // Falls etwas schief läuft gibt die Konsole aus dass es so ist Console.WriteLine(ex.Message); } } } } }

static void Main(string[] args) { Boolean bZahl = false; while (!bZahl) { int Zahl = 0; Console.WriteLine("Bitte eine Zahl eingeben: "); try { Zahl = Int32.Parse(Console.ReadLine()); bZahl = true; } catch { Console.WriteLine("Falsches Format"); } } }
Weiterer Lösungsvorschlag ..
C#-Code

using System; namespace _1_Erfolgreiche_Eingabe_einer_Integer__Zahl { class Program { static void Main(string[] args) { bool check; do { System.Console.WriteLine("Give me an Number!"); string foo = System.Console.ReadLine(); int correct; check = int.TryParse(foo, out correct); } while (!check); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TrainYourProgrammer_1 { class Program { static void Main(string[] args) { Console.WriteLine("Geben sie eine Gradezahl ein!"); check: try { int Zahl = int.Parse(Console.ReadLine()); } catch { Console.WriteLine("Ich sagte, sie sollen eine GRADEzahl eingeben!"); Console.WriteLine("Versuchen sie es nochmal, nicht vergessen GRADE"); goto check; } } } }

using System; namespace ErfolgreicheEingabeEinesIntegers { class Program { static void Main(string[] args) { string eingabe; int number; while (true) { Console.WriteLine("Bitte einen Ganzzahlwert eingeben:"); eingabe = Console.ReadLine(); bool isNumeric = int.TryParse(eingabe, out number); if (isNumeric == false) { Console.WriteLine("Flasches Zeichen eingegeben"); } else { Console.WriteLine("Richtiges Zeichen eingegeben!"); Console.ReadKey(); break; } } } } }

//eingabe prüfen solange kein integer eingegeben wird bool versuch = false; int zahl = 0; string eingabe; while (versuch == false) { Console.WriteLine("Bitte geben Sie eine Ganzzahl ein"); eingabe = Console.ReadLine(); versuch = Int32.TryParse(eingabe, out zahl); if (versuch) { Console.WriteLine("Richtig das war eine Ganzzahl\n"); } else { Console.WriteLine("Falsche eingabe!!\n"); } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Uebung1_EingabeInteger { class Program { static void Main(string[] args) { Console.WriteLine("Geben Sie bitte eine Zahl ein:"); string userinput; bool result = false; int Number; while (result == false) { userinput = Console.ReadLine(); if (Int32.TryParse(userinput, out Number)) { Console.WriteLine("Ihre Zahl ist " + Number); result = true; } else { Console.WriteLine("Bitte geben Sie eine Zahl ein"); } } Console.ReadLine(); } } }

namespace trainyourprogrammer___Aufgabe_1 { class Program { static void Main(string[] args) { bool bEingabe = false; while (bEingabe == false) { Console.WriteLine("Bitte gebe eine ganzzahlige Zahl ein: "); int eingabe; try { eingabe = Convert.ToInt32(Console.ReadLine()); if (eingabe % 2 == 0) { bEingabe = true; Console.WriteLine("Eingabe war erfolgreich"); Console.ReadLine(); } } catch (Exception) { } } Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Eingabe { class Program { static void Main(string[] args) { int i; Console.WriteLine("Zahl eingeben"); while (true) { string ein = Console.ReadLine(); try { i = Int32.Parse(ein); break; } catch { Console.WriteLine("Eingabe nicht erfolgreich, bitte nochmal versuchen"); } } Console.WriteLine("Die Zahl war " + i); Console.ReadLine(); } } }

public void GetInteger() { try { Console.WriteLine("Bitte geben Sie eine ganze Zahl ein."); int number = int.Parse(Console.ReadLine()); Console.WriteLine($"Die eingegebene Zahl lautet: {number}"); Console.ReadLine(); } catch (Exception e) { GetInteger(); } }

using System; namespace training { class Program { static bool checkNumber(string input) { for (int i = 0; i < input.Length; i++) if ((int)input[i] < 48 || (int)input[i] > 57) return false; return true; } static void Main(string[] args) { do { Console.Write("Input: "); } while (!checkNumber(Console.ReadLine())); } } }

public static void zahlEingeben() { Console.WriteLine("Bitte eine Integer-Zahl eingeben"); string eingabe = Console.ReadLine(); if (Int32.TryParse(eingabe, out int result)) { Console.WriteLine("Glückwunsch, Sie haben folgende Zahl eingegeben: " + result); Console.WriteLine("Enter zum Beenden drücken..."); } else { Console.WriteLine("Sie haben keine Integer-Zahl eingegeben!"); zahlEingeben(); } } static void Main(string[] args) { zahlEingeben(); Console.ReadLine(); }

using System; namespace AufgabenTest { class Program { static void Main(string[] args) { int userInput; do { Console.Write("Ein integer-Zahl eingeben: "); } while (!(int.TryParse(Console.ReadLine(), out userInput))); Console.WriteLine("Benutzereingabe = {0}", userInput); Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TrainYourProgrammer1 { class Program { static void Main(string[] args) { int test = 0; while(test == 0) { try { Console.WriteLine("Geben Sie eine Nummer ein: "); test = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(test * 2); Console.ReadLine(); } catch { Console.WriteLine("Ein Fehler ist aufgetreten, bitte geben Sie eine ganze Zahl ein: "); } } } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TrainYourProgrammer1 { class Program { static void Main(string[] args) { Boolean klappt = false; while (klappt == false) { Console.WriteLine("Geben Sie eine ganze Integer Zahl ein: "); klappt = Int32.TryParse(Console.ReadLine(), out int zahl); if (klappt) { Console.WriteLine("Sie haben eine ganze Integer Zahl eingegeben."); } else { Console.WriteLine("Dies ist keine Integer Zahl!"); } } Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Erfolgreiche_Eingabe_eines_Integers { class Program { static void Main(string[] args) { Console.WriteLine("Geben sie eine Ganzzahl ein."); int i = 0; while(!(Int32.TryParse(Console.ReadLine(),out i))) { Console.WriteLine("Geben sie eine Ganzzahl ein."); } Console.WriteLine("Sie haben es Geschafft"); Console.ReadLine(); } } }

using System; public class Program { public static void Main() { bool isValid = false; while (!isValid){ Console.WriteLine("Bitte eine Zahl eingeben: "); string eingabe = Console.ReadLine(); try { int number = int.Parse(eingabe); Console.WriteLine("Die Zahl lautet: "+ number); isValid=true; } catch (FormatException) { Console.WriteLine("Fehler bei der Eingabe: es handelt sich nicht um eine Zahl!"); } } } }

using System; namespace ZahlEingabe { class Program { static void Main(string[] args) { int zahl; do { Console.WriteLine(); Console.Write("Bitte eine Ganzzahl eingeben: "); } while (!Int32.TryParse(Console.ReadLine(), out zahl)); Console.WriteLine("\nEingabe erfolgreich!\nDie Zahl lautet: {0}", zahl); Console.ReadKey(); } } }

class Program { static void Main(string[] args) { Boolean IsValid = false; int num; do { Console.WriteLine("Bitte Integer eingeben: "); IsValid = int.TryParse(Console.ReadLine(), out num); } while (!IsValid); } }
Eine einfache und kurze Lösung:
C#-Code

static void Main(string[] args) { bool isIntegerZahl = false; int Zahl = 0; while (!isIntegerZahl) { Console.WriteLine("Gebe eine Integer Zahl ein: "); isIntegerZahl = int.TryParse(Console.ReadLine(), out Zahl); } Console.WriteLine("Perfekt!"); }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IntegerZahl { class Program { static void Main(string[] args) { int x; Console.Write("Gib eine ganze Zahl ein: "); while (Int32.TryParse(Console.ReadLine(), out x)) { Console.Write("Gib eine ganze Zahl ein: "); } } } }

int b =0; do { int a; string input = Console.ReadLine(); if (Int32.TryParse(input, out a)) { Console.WriteLine("Int " + a); b = 1; } else { Console.WriteLine("Invalid number entered. Please enter number int"); } } while (b == 0); Console.Read();

namespace MyKonsole { using System; static class Program { static void Main() { var erfolgreich = false; while (!erfolgreich) { Console.WriteLine("Bitte eine ganze Integer Zahl eingeben"); erfolgreich = int.TryParse(Console.ReadLine(),out _); Console.Clear(); if (!erfolgreich) Console.WriteLine("Ungültige Eingabe! \n"); } Console.ReadLine(); } } }

namespace ValidInput { class Program { static void Main(string[] args) { int number; bool isValid = true; while (isValid) { try { Console.Write("Please enter a number: "); number = Convert.ToInt16(Console.ReadLine()); if(number % 1 == 0) { Console.WriteLine("Valid Input, your number was: " + number); isValid = false; } } catch { } } Console.ReadLine(); } } }

using System; namespace TrainYourProgrammer { class Program { static void Main(string[] args) { bool check = false; string eingabe = ""; while (check == false) { Console.WriteLine("Bitte geben Sie eine Zahl ein: "); eingabe = Console.ReadLine(); if (UInt32.TryParse(eingabe, out uint r)) { Console.WriteLine("Super, du hast es geschafft"); check = true; } else { Console.WriteLine("Das ist keine Ganze Zahl!"); check = false; } } Console.ReadLine(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace integerzahl { class Program { static void Main(string[] args) { string userInput = ""; int userInt = 0; bool checkInput = false; while (!checkInput) { Console.WriteLine("Eine Integer-Zahl eingeben:"); userInput = Console.ReadLine(); if (!int.TryParse(userInput, out userInt)) { Console.WriteLine("Keine Integerzahl eingeben. Erneut versuchen."); } else { Console.WriteLine("Integerzahl erfolgreich eingegeben: {0}",userInt); checkInput = true; } } Console.ReadKey(); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace C_Sharp_Lernen { class Program { static void Main(string[] args) { string USER_TEXT = string.Empty; int USER_INPUT; while(!int.TryParse(USER_TEXT, out USER_INPUT)) { Console.WriteLine("Bitte eine Zahl eingeben"); USER_TEXT = Console.ReadLine(); } Console.WriteLine("Deine Zahl ist: " + USER_INPUT); Console.Read(); } } }

using System; namespace Exercises { class Program { static void Main(string[] args) { do { Console.WriteLine("\nPlease enter an integer of your choice. This message will repeat itself until task completion."); } while ( !int.TryParse(Console.ReadLine(), out int b)); } } }

using static System.Console; namespace aufgabe_001 { //Erstelle eine Konsolenanwendung, in der der Benutzer (genau) solange aufgefordert wird eine Integer-Zahl einzugeben, //bis dies erfolgreich war. class Program { static void Main() { bool bo_input_ok = false; int input = 0; while (!bo_input_ok) { Write($"give me an integer: "); bo_input_ok = int.TryParse(ReadLine(), out input); if (!bo_input_ok) WriteLine("input not correct!"); } WriteLine($"input correct: {input}"); ReadKey(false); } } }

while (true) { Console.WriteLine("Bitte geben Sie eine Zahl ein"); int userEingabe; bool parseOk = int.TryParse(Console.ReadLine(), out userEingabe); if(parseOk) Console.WriteLine("Ihre Zahl ist: " + userEingabe); }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TYP_Aufgabe_1 { class Program { static void Main(string[] args) { string Eingabe; int PruefZahl; bool EingabeErfolgreich = false; Console.WriteLine("Geben Sie hier bitte eine ganze Zahl ein und bestätigen Sie mit Enter!"); Eingabe = Console.ReadLine(); while (!EingabeErfolgreich) { if (Int32.TryParse(Eingabe, out PruefZahl)) { Console.WriteLine("Die Eingabe war erfolgreich! Das Programm wird sich jetzt beenden!"); EingabeErfolgreich = true; } else { Console.WriteLine("Die Eingabe ist keine ganze Zahl! Bitte erneut versuchen!"); Eingabe = Console.ReadLine(); } } Console.ReadKey(); } } }

using System; using System.Linq; using System.Threading; namespace IntegerZahlEingabe { class Program { static void Main(string[] args) { Console.WriteLine("Bitte geben sie eine Zahl ein:"); Console.Write("Ihre Eingabe: "); bool isZahl = false; string eingabe = Console.ReadLine(); do { try { int eingabeInt = Convert.ToInt32(eingabe); isZahl = true; } catch (Exception) { Console.WriteLine("Ihre Eingabe war keine Zahl"); Thread.Sleep(600); Console.Clear(); Console.WriteLine("Bitte geben sie eine Zahl ein:"); Console.Write("Ihre Eingabe: "); eingabe = Console.ReadLine(); isZahl = false; } } while (!isZahl); } } }

static void Main(string[] args) { int number; string input; bool anotherTurn = false; do //Da auf jeden Fall mindestens 1x gefragt wird. { Console.WriteLine("Bitte geben Sie eine Zahl ein :"); input = Console.ReadLine(); anotherTurn = Int32.TryParse(input, out number); } while (!anotherTurn); //Wenn erfolgreich geparst wurde endet die Schleife sofort Console.ReadKey(); }

using System; namespace IntEingabe { class Program { static void Main(string[] args) { int input = 0; bool fehler = true; Console.Write($"Bitte geben Sie eine integer Zahl ein: "); do { try { input = Convert.ToInt32(Console.ReadLine()); fehler = false; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.Write($"Bitte geben Sie erneut eine integer Zahl ein: "); fehler = true; } } while (fehler); Console.WriteLine($"Ihre integer zahl lautet: {input}"); Console.ReadLine(); } } }

using System; namespace ConsoleApp4 { class Program { static void Main(string[] args) { int Zahlen = 0; while (Zahlen == 0) { Console.Write("Bitte eine Zahl eingeben:"); string Zahl = Console.ReadLine(); Int32.TryParse(Zahl , out Zahlen); if (Zahlen == 0) { Console.WriteLine("Falsche Eingabe"); } else { Console.WriteLine($"{Zahlen}"); } } } } }