如果满足特定条件,我将如何编程我的代码以重新开始?

时间:2019-06-22 22:10:26

标签: c#

为澄清起见,我使用Microsoft Visual Studio并试图将cmd格式转换为可爱的小交互式终端。我是一个基础知识的初学者,并且我知道这对我一个熟练的人来说可能是一个雄心勃勃的项目。目前,我唯一感到困惑的是如何告诉程序我希望它在else if语句的约束下重新开始。

static void Main(string[] args)
        {
            Console.WriteLine("What's your name?");
            string name = Console.ReadLine();
            Console.WriteLine("Hello, " + name);
            Console.WriteLine("What's your age?");
            string age = Console.ReadLine();
            Console.WriteLine("Your name is " + name + " and you are " + age + " years old, Correct?");
            string val = Console.ReadLine();
            if (val == "yes")
            {
                Console.WriteLine("Data confirmed, thank you for your cooperation!");
            }
            else if (val == "no")
            {       //I would like to restart the code if this condition is met
                Console.WriteLine("Incorrect data provided, please try again");
            }
            else
            {       //The issue with this line is that I want it to just ask if the data is correct again
                Console.WriteLine("That is not a valid response, try answering with a yes or no");
            }

2 个答案:

答案 0 :(得分:1)

尝试类似这样的方法。基本上,您是将代码包装在一个循环中(在这种情况下,是一个“ do ... while”循环,并添加一个标志,指示程序是否应该“重新开始”。

static void Main(string[] args)
{
    bool finished;

    do
    {
        finished = true;
        Console.WriteLine("What's your name?");
        string name = Console.ReadLine();
        Console.WriteLine("Hello, " + name);
        Console.WriteLine("What's your age?");
        string age = Console.ReadLine();
        Console.WriteLine("Your name is " + name + " and you are " + age + " years old, Correct?");
        string val = Console.ReadLine();
        while(val != "yes" && val != "no") {
            Console.WriteLine("That is not a valid response, try answering with a yes or no");
            val = Console.ReadLine();
        }
        if (val == "yes") {
            Console.WriteLine("Data confirmed, thank you for your cooperation!");
        }
        else if (val == "no") {
            Console.WriteLine("Incorrect data provided, please try again");
            finished = false;
        }
    } while(!finished);
}

答案 1 :(得分:0)

您可以将代码分解为单独的方法,然后从Main()方法中调用它。然后,在您的else语句中,您可以让新方法本身进行调用,以便再次执行。 (免责声明:自编写C#以来已经有很长时间了。)

static void Main(string[] args) {
  askName();
}

static void askName() {
  Console.WriteLine("What's your name?");
  string name = Console.ReadLine();
  Console.WriteLine("Hello, " + name);
  Console.WriteLine("What's your age?");
  string age = Console.ReadLine();
  Console.WriteLine("Your name is " + name + " and you are " + age + " years old, Correct?");
  string val = Console.ReadLine();

  if (val == "yes") {
    Console.WriteLine("Data confirmed, thank you for your cooperation!");
  } else  if (val == "no") {
    askName();
  } else {
    //The issue with this line is that I want it to just ask if the data is correct again
    Console.WriteLine("That is not a valid response, try answering with a yes or no");
  }
}