000
000

Reputation: 55

Jumping in C# using console application

How can I make the program go back to an other step? For instance:

        Console.WriteLine("Hi.");
        string eersteAntwoord = Console.ReadLine();
        if (eersteAntwoord == "Hi" || eersteAntwoord == "hi")
        {
            Console.WriteLine("How are you doing?");
            {
                string begroeting = Console.ReadLine();
                if (begroeting == "I'm good")
                {
                    Console.WriteLine("Good");
                }
                else if (begroeting == "hi")
                {
                    Console.WriteLine(""); // I want it to go from here to the first step.

Any idea how I can do that? }

Upvotes: 1

Views: 342

Answers (5)

Philip.ie
Philip.ie

Reputation: 1506

And another....

static void Main(string[] args)
    {
        Console.WriteLine("Hi.");
        string eersteAntwoord = String.Empty;
        string begroeting = String.Empty;
        bool stop = false;
        while (!stop)
        {
            eersteAntwoord = Console.ReadLine();
            if (eersteAntwoord.Equals("HI", StringComparison.InvariantCultureIgnoreCase))
            {
                Console.WriteLine("How are you doing?");
                {
                    begroeting = Console.ReadLine();
                    if (begroeting.Equals("I'M GOOD", StringComparison.InvariantCultureIgnoreCase))
                    {
                        Console.WriteLine("Good");
                    }
                    else if (begroeting == "HI")
                    {
                        Console.WriteLine("You hurt your hand."); // I want it to go from here to the first 
                    }
                }
            }
            if (eersteAntwoord.Equals("STOP", StringComparison.InvariantCultureIgnoreCase)
                || begroeting.Equals("STOP", StringComparison.InvariantCultureIgnoreCase))
                    break;
        }
    }

Upvotes: 0

albertjan
albertjan

Reputation: 7817

This reads lines from the Console until the user writes "bye" or "Bye".

(In het nederlands: Dit leest regels van de console tot dat de gebruiker "bye" of "Bye" tiept.)

string antwoord = "";
while (antwoord != "bye" && antwoord != "Bye")
{
    antwoord = Console.ReadLine();
}

Upvotes: 0

user596075
user596075

Reputation:

string begroeting;

while(begroeting.ToLower() != "some string you want to stop loop execution")
{
   Console.WriteLine("Hi."); 
    string eersteAntwoord = Console.ReadLine(); 
    if (eersteAntwoord == "Hi" || eersteAntwoord == "hi") 
    { 
        Console.WriteLine("How are you doing?"); 

        begroeting = Console.ReadLine(); 
        if (begroeting == "I'm good") 
        { 
            Console.WriteLine("Good"); 
            break; // this would be if you want to get out of your loop
        } 
        else if (begroeting == "hi") 
        { 
            Console.WriteLine(""); 
            continue; // go to the next iteration of the while loop
        }
    }
}

Upvotes: 3

Stefan Koenen
Stefan Koenen

Reputation: 2337

maybe you can use goto, and this is maybe better:

Equals(root2, StringComparison.OrdinalIgnoreCase);

        Console.WriteLine("Hi.");
        string eersteAntwoord = Console.ReadLine();
        if (eersteAntwoord.Equals("hi", StringComparison.OrdinalIgnoreCase))
        {
             while(!HowAreYouDoing());
        }


bool howAreYouDoing()
{
    Console.WriteLine("How are you doing?");
    string begroeting = Console.ReadLine();

    if (begroeting == "I'm good")
    {
        Console.WriteLine("Good");
        return true;
    }
    else if (begroeting == "hi") 
    {
        Console.WriteLine("You hurt your hand.");
        return false;
    }
}

Upvotes: 0

Shai
Shai

Reputation: 25595

string sLine = Console.ReadLine();
while(sLine.ToUpper() == "HI")
{
    .... (DO STUFF)
    sLine = Console.ReadLine();
}

Upvotes: 0

Related Questions