如何循环回到'if'分支的开头?

时间:2015-07-01 21:05:50

标签: c#

如果满足'else'条件,我想循环回'if'条件的开头。我是C#编程的新手,如果我诚实的话,我会挣扎很多。对不起,如果之前有人询问,我做了搜索但没有找到任何内容。

这是我的(杂乱)代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tinkering
{
    class Program
    {
        static void Main(string[] args)
        {

            {
                Console.WriteLine("Let's go on an adventure shall we?\n\nLet's start with your name:");
                string userName = Console.ReadLine();

                Console.WriteLine("\nHi " + userName);
                Console.WriteLine("\nIs that a girls' name?\n");
                Console.ReadLine();

                Console.WriteLine("\nI see\n");
                Console.WriteLine("Well anyway, let's get started shall we?\n");
                Console.WriteLine("Would you care to go Left or Right " + userName);

                while (userInput)
                {

                    string userInput = Console.ReadLine();
                    if (userInput == "left")
                    {
                        Console.WriteLine("Left it is!\nYou see a long dark corridor. There is a sudden empty feeling in your stomach, kind of like that weird feeling where it feels empty but you know you could shit...");
                        Console.ReadLine();
                    }

                    else if (userInput == "right")
                    {
                        Console.WriteLine("Ok let's head right\n");
                        Console.WriteLine("\nIt looks as though we have a locked door here...");
                        Console.WriteLine("Unfortunately" + userName);
                        Console.WriteLine("that is the end of this shitty little game (I couldn't be bothered writing anymore code for it lol");
                        Console.ReadLine();
                    }

                    else
                    {
                        Console.WriteLine("Just type 'left' or 'right' please");
                        Console.ReadLine();
                    }
                }

3 个答案:

答案 0 :(得分:1)

您在while循环开始时使用console.ReadLine()。所以从if块的末尾删除它们,否则if block和else块。然后你的代码应该自动回到if语句

 while (userInput)
            {
                string userInput = Console.ReadLine();
                if (userInput == "left")
                {
                    Console.WriteLine("Left it is!\nYou see a long dark corridor. There is a sudden empty feeling in your stomach, kind of like that weird feeling where it feels empty but you know you could shit...");
                }

                else if (userInput == "right")
                {
                    Console.WriteLine("Ok let's head right\n");
                    Console.WriteLine("\nIt looks as though we have a locked door here...");
                    Console.WriteLine("Unfortunately" + userName);
                    Console.WriteLine("that is the end of this shitty little game (I couldn't be bothered writing anymore code for it lol");
                }

                else
                {
                    Console.WriteLine("Just type 'left' or 'right' please");
                }

答案 1 :(得分:0)

假设您的while条件正常,您可以在break;"left"个案例的末尾添加"right"。这将使你脱离循环并继续前进。

一个简单的场景是这样的:

string userInput;
while (true){
    userInput = Console.ReadLine();
    if (userInput == "left"){
        //do stuff
        break;
    } else if (userInput == "right"){
        //do stuff
        break;
    } else {
        //do stuff
    }
}

答案 2 :(得分:0)

最佳实践还包括:a)使用Swtich vs. If语句和b)避免使用文字常量。块结束注释是一种罕见的“最佳实践”,但我个人发现它们在使用时显着提高了可读性,反之亦然(特别是遗留代码中的“最差实践”“长”块)。

实施例

        const string Direction_Left = "left";
        const string Direction_Right = "right";

        string userInput;

        while (true)
        {

            userInput = Console.ReadLine();

            switch (userInput)
            {

                case Direction_Left:
                {
                    //do stuff
                    break;
                } // case Direction_Left:

                case Direction_Right:
                {
                    //do stuff
                    break;
                } // case Direction_Right:

                default:
                {
                    //do stuff
                    continue;
                } // default:

            } // switch (userInput)

        } // while (true)