使用控制台应用程序跳转到C#

时间:2011-12-01 14:54:21

标签: c#

如何让程序回到另一个步骤? 例如:

        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.

知道我该怎么做吗?                     }

5 个答案:

答案 0 :(得分:3)

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
        }
    }
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这将从控制台读取行,直到用户写入“bye”或“Bye”。

(在hetlands:Dit leest regels van de console totd de gebruiker“bye”of“Bye”tiept。)

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

答案 3 :(得分:0)

也许你可以使用goto,这可能更好:

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;
    }
}

答案 4 :(得分:0)

另一个......

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;
        }
    }