Home > Software engineering >  How to go back into Console.ReadLine() with return
How to go back into Console.ReadLine() with return

Time:11-23

I don't get, how i can fix that. I'm coding "Higher / Lower". I commented where the problem is. please don't spoiler the full solution of that game :) (sry for my bad english)

static void Main(string[] args)
        {
            Console.WriteLine("Higher ! Lower Game");
            Console.WriteLine("\nSchwierigkeitsstufen:\nEasy: 1 - 100\nMedium: 100 - 1000\nHard: 1000 - 10000");
            Console.WriteLine("\nWähle ein Level!");
            string auswahl = Convert.ToString(Console.ReadLine());
            Console.WriteLine("\nDu hast "   auswahl   " gewählt\nBist du sicher?\n'Ja' / 'Nein'");
            string confirm = Convert.ToString(Console.ReadLine());
  
            /// TEXT
            
            /// PROZESS
            if (confirm == "Ja")
            {
                if (auswahl == "Easy")
                {
                    Console.WriteLine("\nGebe eine Zahl ein!");
                    int eingabe = Convert.ToInt32(Console.ReadLine());

                    Random rnd = new Random();
                    int rmd = rnd.Next(0, 99);


                    while (eingabe != rmd)
                    {
                        int eingabe2 = Convert.ToInt32(Console.ReadLine());
                        while (eingabe < rmd)
                        {
                            Console.WriteLine("Higher");
                            return; /// Here, it should go back to "eingabe2" to let the user guess again. if im trying: "return eingabe2;" there comes the error "CS0127".
                        }
                        while (eingabe > rmd)
                        {
                            Console.WriteLine("Lower");
                            return; /// Here, it should go back to "eingabe2" to let the user guess again. if im trying: "return eingabe2;" there comes the error "CS0127".
                        }

                    }
                }
            }
        }

CodePudding user response:

Do you make your code work the first thing is to remove the nested loop. I may be wrong but you should not need it. Just reassign your value.

Somethings you want to dig:

  • int.TryParse vs Convert.ToInt32()
  • while vs do...while
  • Enum type for you level (Level.Easy, Level.Medium, Level.Hard) and for Ja/Nein (Answer.Yes, Answer.No)
  • Guard pattern (return early to avoid nesting logic)

Code:

static void Main(string[] args)
{
    Console.WriteLine("Higher ! Lower Game");
    Console.WriteLine("\nSchwierigkeitsstufen:\nEasy: 1 - 100\nMedium: 100 - 1000\nHard: 1000 - 10000");
    Console.WriteLine("\nWähle ein Level!");
    string auswahl = Convert.ToString(Console.ReadLine());
    Console.WriteLine("\nDu hast "   auswahl   " gewählt\nBist du sicher?\n'Ja' / 'Nein'");
    string confirm = Convert.ToString(Console.ReadLine());
    /// TEXT
     
    /// PROZESS
    if (confirm == "Ja")
    {
        if (auswahl == "Easy")
        {
            Console.WriteLine("\nGebe eine Zahl ein!");
            var rnd = new Random();
            var rmd = rnd.Next(0, 99);
            var eingabe = -1;
            while (eingabe != rmd)
            {
                eingabe = Convert.ToInt32(Console.ReadLine());
                if (eingabe < rmd)
                {
                    Console.WriteLine("Higher");
                /// Here, it should go back to "eingabe2" to let the user guess again. if im trying: "return eingabe2;" there comes the error "CS0127".
                }

                if (eingabe > rmd)
                {
                    Console.WriteLine("Lower");
                /// Here, it should go back to "eingabe2" to let the user guess again. if im trying: "return eingabe2;" there comes the error "CS0127".
                }
            }
        }
    }
}

CodePudding user response:

C# offers the goto command, might be useful for your usecase. For more details have a look at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/goto

  •  Tags:  
  • c#
  • Related