C# 结束循环以返回到下一个序列

C# 结束循环以返回到下一个序列,c#,C#,我想用C#做一次文字冒险。我只写了几天的代码,我找不到怎么做。 我试着问一个是或不是的问题,是会转到故事中的下一个问题。如果是否定的,我希望它开始一个循环,直到它迫使玩家输入正确的答案(是)。在用户输入yes之后,我如何让它退出循环并转到故事的下一部分?如果这不合理,问我,我会尝试重新解释。 下面是我如何让它至少进入下一节的。我希望它循环,直到用户说是。我希望它能循环 using System; namespace Game.of.words.forgotten.period { clas

我想用C#做一次文字冒险。我只写了几天的代码,我找不到怎么做。 我试着问一个是或不是的问题,是会转到故事中的下一个问题。如果是否定的,我希望它开始一个循环,直到它迫使玩家输入正确的答案(是)。在用户输入yes之后,我如何让它退出循环并转到故事的下一部分?如果这不合理,问我,我会尝试重新解释。 下面是我如何让它至少进入下一节的。我希望它循环,直到用户说是。我希望它能循环

using System;

namespace Game.of.words.forgotten.period
{
  class Program
  {
    public static player currentplayer = new player();

    static void Main(string[] args)
    {
      Console.Clear();
      Start();
      Encounters.Firstencounter();
    }

    static void Start()
    {
      Console.WriteLine("Game Of Words The Forgotten Period");
      Console.WriteLine("Welcome to my world. What is your name? ");
      string currentplayer = Console.ReadLine();
    
      Console.Clear();
            
      Console.WriteLine("We are happy to see you here " + currentplayer + " !");
      Console.WriteLine("I bet you are wondering where you are and why you are here. Is that correct? (Yes) or (No)");
      string input = Console.ReadLine();
      if (input.ToLower() == "yes")
      {
        //Yes
        Console.WriteLine("I thought as much.I am incredibly skilled at the confusion arts");
      }
      else 
      {
        //no
        Console.WriteLine("Well I just assumed because... I mean I took all this time to make this a crazy experience to");
        Console.WriteLine(" confuse you but I guess you are too smart for that.");
        Console.WriteLine("Please just say yes.");
        Console.ReadLine();
              
        if (input.ToLower() == "no")
        {
          Console.WriteLine("Okay fine. You can be like that");
        }
        else 
          Console.Write("I thought as much. I am incredibly skilled at the confusion arts.");

        Console.ReadKey();
        Console.Clear();
      }

      Console.ReadLine();
      Console.WriteLine("As you see we are in the middle of no where. Surounded by Tall mountains and no trees.");
      Console.WriteLine("I bet you can feel the slight breeze and smell the fragrece of the flowers all around us");
      Console.WriteLine("Do you want me to telaport us into town or do you walk through the flowers and watch the clouds for a while? ");
      Console.ReadLine();
      Console.Clear();
    }
  }
}
这里是另一个例子。我不明白什么

class Program
{

    static void Main(string[] args)
    {
        Console.Write("hello Player what is your name? ");
        String Name= Console.ReadLine();
        string j = ("June");
        if (Name != j)
        {
            Console.WriteLine("Hello " + Name);
            while (Name != j)
            {              
                Console.WriteLine("Thats not your name");
                // Creates endless loop. If I add a readline it will
                //print hello michael and then that is not your name will
                //Repeat on every enter.
                //If I put a break; it just prints both and ends. 
            }
            Console.ReadLine();
        }
        Console.ReadLine();
    }
}

在第二个示例中,您实际上不需要if语句,可以使用while循环

string exitName = "exit";
Console.Write($"hello Player what is your name? (to skip the while loop you must type {exitName}) {Environment.NewLine}");
String Name = Console.ReadLine();
while (Name != exitName)//while the name does not equal 'exit' you will stay in the loop
{
    Console.WriteLine("You did not exit!");
    Console.WriteLine($"You entered {Name} you must enter {exitName} to exit this loop!");
                    Name = Console.ReadLine();
}
    
Console.WriteLine("press any key to end game");
Console.ReadLine();
在您的第一个示例中,您有以下行

Console.WriteLine("Please just say yes.");
Console.ReadLine();
这应该是

Console.WriteLine("Please just say yes.");
input = Console.ReadLine();

否则,您将不会再次分配任何输入。

为每个问题指定一个数字。如果玩家说“不”,将当前问题转移到循环开始的问题。我们没有足够的上下文。显示导致问题的代码。欢迎-请提供您尝试过的代码。您的代码中没有任何循环。您可能需要一个
while(something is true){…}
类型的循环来满足您的请求。顺便说一句,您的代码中将currentplayer声明为一个玩家类和一个字符串。@LarsTech哦,是的,我在学习一个教程,他为玩家创建了一个类文件。我试了一会儿,但一旦我进去,我似乎无法阻止它。如果我输入yes,它会继续,就像我说no一样。这就是为什么我放弃了,改成了这个。哈哈