结束循环返回下一个序列

时间:2020-06-25 22:49:32

标签: c#

我试图用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();
    }
}

1 个答案:

答案 0 :(得分:0)

在第二个示例中,您实际上并不需要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();

否则,您无需分配任何内容即可再次输入。