而Loop switch语句直到语句为false

时间:2014-06-16 18:28:39

标签: c#

我创建了两个使用相同属性的类。从第二类继承的函数GenerateRamdon的方向类称为健康。我发现的问题是,当它运行GenerateRamdon函数中的代码时,它没有到达switch语句。在查看断点和正在传递的信息时,Ramdom方法工作正常并生成正确的数字,当删除while循环时,它将转到正确的if语句。

private void GenerateRamdon()
{
    while (StartHealth < 0)
    {
        Random DirectionRandon = new Random();
        int GetDirect = DirectionRandon.Next(0, 100);

        if (GetDirect <= 20)
        {
            Console.WriteLine("Would you like to move straight, Right or Left");
            string FirstDirect = Console.ReadLine();

            while (FirstDirect.Equals(true))
            {
                switch (FirstDirect)
                {
                    case "Straight":
                        Console.WriteLine("You walk straight forward");
                        break;

                    case "Right":
                        Console.WriteLine("You walk to your right");
                        break;

                    case "Left":
                        Console.WriteLine("You walk to your left");
                        break;

                    default:
                        Console.WriteLine("Please enter Vailid direction");
                        break;
                }
            }
        }
        else if (GetDirect > 21 && GetDirect <= 40)
        {
            Console.WriteLine("Would you like to move stay, back or Left");
            string SecontDirect = Console.ReadLine();

            while (SecontDirect.Equals(true))
            {
                switch (SecontDirect)
                {
                    case "Stay":
                        Console.WriteLine("You stand still");
                        break;

                    case "Back":
                        Console.WriteLine("You walk back to where you came but it looks all different");
                        break;

                    case "Left":
                        Console.WriteLine("You walk to your left");
                        break;

                    default:
                        Console.WriteLine("Please enter Vailid direction");
                        break;
                }
            }
        }
        else if (GetDirect > 21 && GetDirect <= 60)
        {
            Console.WriteLine("Would you like to move Back, Right or Left");
            string ThridDirect = Console.ReadLine();

            while (ThridDirect.Equals(true))
            {
                switch (ThridDirect)
                {
                    case "Back":
                        Console.WriteLine("You walk back to where you came but it looks all different");
                        break;

                    case "Right":
                        Console.WriteLine("You walk to your right");
                        break;

                    case "Left":
                        Console.WriteLine("You walk to your left");
                        break;

                    default:
                        Console.WriteLine("Please enter Vailid direction");
                        break;
                }
            }
        }
        else if (GetDirect > 61 && GetDirect <= 80)
        {
            Console.WriteLine("Would you like to move back, straight or Right");
            string FourthDirect = Console.ReadLine();

            while (FourthDirect.Equals(true))
            {
                switch (FourthDirect)
                {
                    case "Back":
                        Console.WriteLine("You walk back to where you came but it looks all different");
                        break;

                    case "Straight":
                        Console.WriteLine("You walk straight forward");
                        break;

                    case "Right":
                        Console.WriteLine("You walk to your right");
                        break;

                    default:
                        Console.WriteLine("Please enter Vailid direction");
                        break;
                }
            }
        }
        else
        {
            Console.WriteLine("Would you like to move straight, Right, Stay or Left");
            string FifthDirect = Console.ReadLine();

            while (FifthDirect.Equals(true))
            {
                switch (FifthDirect)
                {
                    case "Straight":
                        Console.WriteLine("You walk straight forward");
                        break;

                    case "Right":
                        Console.WriteLine("You walk to your right");
                        break;

                    case "Stay":
                        Console.WriteLine("You stand still");
                        break;

                    case "Left":
                        Console.WriteLine("You walk to your left");
                        break;

                    default:
                       Console.WriteLine("Please enter Vailid direction");
                       break;
                }
            }
        }
    }
}

健康状况下的第二个类具有此功能,该功能保存角色的开始健康状况的信息,并且我希望方向保持循环直到用户生命(HP)命中为零。

private void StartHP()
{
    this.StartHealth = 100;
    Console.SetCursorPosition(y,x);
    Console.Clear();
    Console.WriteLine("HP: " + StartHealth);
    Console.WriteLine("");
    Console.WriteLine("");
}

当我循环遍历switch语句时,代码未被访问且控制台应用程序正在关闭。我只是想知道为什么这是和如何解决问题的指导。感谢您提供的任何信息。

1 个答案:

答案 0 :(得分:0)

这里有几个问题;让我们首先确保我们的输入请求在循环中,并且我们有一个布尔值(true / false)来存储程序是否满意输入:

Console.WriteLine("Would you like to move straight, Right or Left");
bool isInputValid = false; // stores the validity of the input, reusable
string FirstDirect = "moonwalk"; // this will be overwritten inside of the loop
while (!isValidInput) // same as: while (isValidInput == false)
{
  firstDirect = Console.ReadLine();
  switch (FirstDirect)
  {
    case "Straight":
      Console.WriteLine("You walk straight forward");
      isValidInput = true;
      break;
    case "Right":
      Console.WriteLine("You walk to your right");
      isValidInput = true;
      break;
    case "Left":
      Console.WriteLine("You walk to your left");
      isValidInput = true;
      break;
    default:
      Console.WriteLine("Please enter Valid direction");
      isValidInput = false; // not necessary, but there you go
      break;
    }
  }
}
isValidInput = false; // reset for next loop

我们实际上可以在后续循环中重复使用方向指示符(firstDirect我将更改为inputDirection)变量,除非您想要保留数据以了解他们之前的选择。