如何在交互式故事场景中清除控制台并执行特定代码块

How to do I clear the console and execute a specific block of code in an interactive story scenario

我整个高中都在学习计算机编程 classes,现在我已经高三了,我们终于开始实际输入代码和编写程序了。我的 class 基于 C#,我们最近(上周一)才开始学习实际代码而不是理论。

我正努力走在潮流的前面,我已经 Console.WriteLine();并分配基本变量、整数等。基本的事情,我知道,但我们都从某个地方开始。我想走在潮流的前面,所以我决定开始制作一个非常简单的基于文本的交互式故事,如果你愿意的话。它与战斗系统或统计数据无关。只需提出一个问题并提供选项供用户选择。

    Console.WriteLine("Please enter your name: "); 
    string name = Console.ReadLine();
    Console.Clear();
    Console.Write("Hi " + name);
    Console.WriteLine(", how are you?);
    Console.WriteLine();
    Console.WriteLine("1.) I'm well.");
    Console.writeLine("2.) I'm not well.");
    string areYouWell = Console.ReadLine(); 

    if (areYouWell=="1")
       {
           Console.WriteLine("I'm Well.");
       }
    else if (areYouWell== "2")
       {
           Console.WriteLine("I'm not well.");
       }
    else
       {
         // this is where my question is. i want this else statement to
            re-execute the Console.Write("Hi, " +name); line

我的问题是:如果用户输入未分配的值,我想让用户返回到代码块的开头,我该怎么做?我很少使用 batch 和那里的 goto 功能,但在我对该主题的研究中,我了解到 goto 基本上是第八大罪过。什么是替代方案?

这是我现在的代码:

    namespace NameGame
    {

     class Jade
     {
        public static void Main(string[] args)
        {
            /*Tx-Y
             * T= Tier  x=tier level     Y=option
             *
             * 
             * 
             * */
            Start: Console.WriteLine("Hi, welcome to JADE. Im sure I could've come up with a clever acronymn.. but I didn't.");
            Console.WriteLine("JADE is the project that I work on in my free time to practice programming. I hope you enjoy.");
            Console.WriteLine();
            Console.WriteLine("(At any time press \"Esc\" to close the program or type \"Ctrl\" to come back to this screen");
            Console.WriteLine("What is your name?");
            string usersName = Console.ReadLine();
            Console.Clear();
            Console.Write("It's nice to meet you " + usersName);
            Console.WriteLine("! My name is Jade, a programmable coversationalist!");
            Console.Write("I would LOVE to have a conversation with you! Afterall, it ");
            Console.WriteLine("is what I was made for!");
            Console.WriteLine("\n");
            Console.WriteLine("So, what do you want to talk about?");
            Console.WriteLine("\n");
            Console.WriteLine("(Press a corresponding number on the keyboard and press enter)");
            Console.WriteLine("1.) How your day is going");// start of tier one questions | Jade T1-O1
            Console.WriteLine("2.) Relationships"); //Jade T1-O2   || For relationships use if statement to personalize based on a hand full of names or default to a simple message
                                                //add more dialogue branches 

            string questionOne = Console.ReadLine(); //declaring string questionOne for the first question jade asks the user
            if (questionOne == "1")
            {

                Console.Clear();
                Console.WriteLine("OOO! I love talking about people's days! What have you done today?");
                Console.WriteLine("\n");
                Console.WriteLine("1.) School work mostly"); //Tier2-Question1-Option1
                Console.WriteLine("2.) Took a test or quiz"); //T2-Q1-O2
                Console.WriteLine("3.) Died on the inside due to school's neverending drag of despair."); //T2-O3
                string T2Q1 = Console.ReadLine();
               // continue this path
            }
            else if(questionOne=="2")
            {
                Console.Clear();
                Console.WriteLine("OOO! I love a good romance! So, tell me. Is it good or bad?");
                Console.WriteLine("\n");
                Console.WriteLine("\n");
                Console.WriteLine("1.) Good"); //T2-Q2-O1
                Console.WriteLine("2.) Bad"); //T2-Q2-O2
                string T2Q2 = Console.ReadLine();
                //continue this path
            }
            else
            {
                return (if (questionOne == "1"));
            }

            Console.ReadKey();
        }
    }
}

抱歉,冗长 post,我已经搜索了一段时间了,哈哈

有很多不同的方法可以做到这一点。最容易理解的一种是 do ... while 循环。

string questionOne;

do
{
    Console.WriteLine("(Press a corresponding number on the keyboard and press enter)");
    Console.WriteLine("1.) How your day is going");
    Console.WriteLine("2.) Relationships");

    questionOne = Console.ReadLine();
    if (questionOne == "1")
    {
        // questions about how your day is going
    }
    else if (questionOne == "2")
    {
        // relationships questions
    }
    else
    {
        Console.WriteLine("I didn't understand your answer. Try again.");
    }
} while (questionOne != "1" && questionOne != "2");

只要 questionOne 不是“1”或“2”,do ... while 循环就会重复。所以如果用户输入“3”,程序会输出"I didn't understand your answer",然后遇到while语句。由于 questionOne 既不是“1”也不是“2”,它将回到循环的开始。