c#循环询问多个问题

时间:2016-07-28 18:38:05

标签: c# loops

我很困惑,这只是我编码/脚本编写的第二个项目,我不明白如何让玩家有多种选择供选择。循环在第二个或第三个问题之后返回而没有继续,并且它一直在跳过几行代码,只是想知道我做错了什么。

    static void Main(string[] args)
    {
        Random rng = new Random();
        int stuff = 0;
        bool gameGo = true;
        string yourAnswer = "";
        string choice1 = "";
        string choice2 = "";
        string choice3 = "";
        string choice4 = "";
        int healthPoints = 100;
        int deathNumber = rng.Next(0, 10);
        deathNumber = deathNumber = 0;
        int stabbed = rng.Next(1, 4);
        string name = "";
        int trap = rng.Next(1, 5);
        int arrow = rng.Next(7, 14);
        int menu = 0;
        int potion = rng.Next(1, 10);
        int flyHealth = rng.Next(5, 10);
        int flyAttack = rng.Next(3, 5);
        int tribeAttack = rng.Next(80, 100);
        int playersSword = rng.Next (3, 5);
        while(true)
        {
            break;
        }
 while (gameGo || stuff == 1 )
 {

 Console.Write("You have ");
 Console.Write(healthPoints);
 Console.Write(", health points left");
 Console.Write(" \nIf your health points drops to ");
 Console.Write(deathNumber);
 Console.Write(", you will DIE!");
 Console.WriteLine("\nWell now that that's out of the way, excuse me for being so rude but,\nmay i have your name? Please hit enter after to continue.");
 name = Console.ReadLine();
 Console.Clear();


 if (name == "Dustin")
 {
     Console.Write("You have");
     Console.Write(healthPoints);
     Console.Write(", health points left");
     Console.WriteLine("\nI am so proud of you for figuring out your name, {0}", name);
     Console.WriteLine("Shall we move on to the story?\n Hit Enter to continue.");
     Console.ReadLine();
     menu = 2;

 }

 else if (name != "Dustin")
 {
     healthPoints -= stabbed;
     Console.Clear();
     Console.WriteLine("Seriously you cant figure out your name, how dumb are you?");

 }
 if (menu == 2)
 {
     Console.WriteLine("You see a cave in the distance.");
     Console.WriteLine(" You have to make a choice.");
     Console.WriteLine("Do you go in to explore or do you pass on by?");
     Console.WriteLine("a) Do you go in\nOr B) do you pass by?");
     choice1 = Console.ReadLine();


 }
 if (choice1 == "a")
 {
     Console.WriteLine("You enter a dark cave with no idea where to go but you found a sword.");
     Console.WriteLine("The sword can do 1-5 damage.\n This is your only defense.");
     Console.Write("\nTap enter once to continue");
     Console.ReadLine();
     menu = 3;
     Console.Clear();
 }
 if (choice1 == "b")
 {
     Console.WriteLine("What, are you scared of the dark?");
     healthPoints -= stabbed;
     Console.WriteLine("A masked man stabbed you for not going in the cave and says");
     Console.Write("you now have,");
     Console.Write(healthPoints);
     Console.Write(" health points left");
     Console.WriteLine("Hit enter to continue.");
     Console.ReadLine();
     menu = 4;
     continue; 
 }
 {
     while (menu == 3)
     {
         Console.WriteLine("You wander further in the cave and look around");
         Console.WriteLine("You see a torch on the wall");
         Console.WriteLine("You have another choice.\nDo you a) Light the torch \n or b) Leave it unlit");
         choice2 = Console.ReadLine();
         Console.Clear();
     }
     if (choice2 == "a")
     {
         Console.WriteLine("Well done my good sir, you have brought light to this wretched cave.");
         Console.WriteLine("You move along slowly and encounter a giant fly.");
         Console.Clear();
         menu = 5;

     }
     if (choice2 == "b")
     {
         Console.WriteLine("What do you enjoy being in the dark?");
         healthPoints -= stabbed;
         Console.WriteLine("A masked man snuck up on you and stabbed you in the dark");
         Console.Write("you now have,");
         Console.Write(healthPoints);
         Console.Write(" health points left");
         Console.ReadLine();
         Console.WriteLine("You move along slowly in the dark and encounter a giant fly.");
         Console.ReadLine();
         Console.Clear();
         menu = 6;
     }
 }
 {
     while (menu == 4)
     {
         Console.WriteLine("You chose to keep walking along and not go into the cave.");
         Console.WriteLine("You stumble across a large valley.");
         Console.WriteLine("Time for another choice.");
         Console.WriteLine("Do you a) Climb down the valley hoping for cool treasure?\nOR b) Keep walking along?");
         choice3 = Console.ReadLine();
         Console.Clear();
     }

     if (choice3 == "a")
     {
         Console.WriteLine("You decided to go into the valley");
         Console.WriteLine("Little did you know there was a trap that got sprung as you climbed down");
         healthPoints -= trap;
         Console.Clear();
         menu = 7;

     }
     if (choice3 == "b")
     {
         Console.WriteLine("How much more boring can you get, do you ever go on real adventures.");
         Console.WriteLine("as you are walking along a stray arrow comes out of no where and strikes you in the leg.");
         healthPoints -= arrow;
         Console.Clear();
         menu = 8;
     }
 }
 {
     while (menu == 5)
     {
         //giant fly fight
         Console.Write("The giant fly swoops in to attack");
         flyAttack -= healthPoints;
         Console.WriteLine("The fly does {0} damage to your health", flyAttack);
         Console.Write("You now have,");
         Console.Write(healthPoints);
         Console.Write(" health.");
         Console.WriteLine("You attack back, with your sword you found");
         playersSword -= flyHealth;
         Console.WriteLine("You did {0} damage to the flys health points.", playersSword);
         menu = 9;


     }
     while (menu == 6)
     {
         Console.Write("The giant fly swoops in to attack");
         flyAttack -= healthPoints;
         Console.WriteLine("The fly does {0} damage to your health", flyAttack);
         Console.Write("You now have,");
         Console.Write(healthPoints);
         Console.Write(" health.");
         Console.WriteLine("You attack back, with your sword you found");
         playersSword -= flyHealth;
         Console.WriteLine("You did {0} damage to the flys health points.", playersSword);
         menu = 9;
     }
     while (menu == 7)
     {
         Console.WriteLine("You reach the bottom and come across a tribe of people.");
         Console.WriteLine("This tribe has never seen another human before.");
         Console.WriteLine("Hit enter to continue.");
         Console.ReadLine();
         Console.Clear();
         Console.WriteLine("You hesitate for a bit wondering what to do. \nWhen all of a sudden the tribe attacks.");
         healthPoints -= tribeAttack;
         Console.Write("You have ");
         Console.Write(healthPoints);
         Console.Write(", health points left");

     }
     if (menu == 8)
     {
         //kept walking 
     }
     if (menu == 9)
     {

1 个答案:

答案 0 :(得分:0)

while循环发生,直到满足条件。

这样写的东西(取自上面的例子)永远不会停止。

while (menu == 3 {
    // do stuff here, but menu never changes
}

menu为3时输入循环。menu永远不会从循环中更改,因此它永远不会从3更改 - 因此循环将永远不会退出。它将简单地从循环开始处开始,直到menu不是3。

为了进一步说明,if语句会导致代码根据条件执行。与while循环不同,执行的代码块不会循环。如果条件为假,则不执行块内的代码。如果条件为真,则块中的代码仅执行一次。

一旦你想到事情如何发展,你就能取得一些进展。为了解决这个问题,如果您使用的是现代IDE,则可以在开头设置断点并逐步执行该程序,观察决策如何一次一行。