【问题标题】:How to start a loop over again after a user pressed a wrong key?用户按错键后如何重新开始循环?
【发布时间】:2013-12-21 16:37:48
【问题描述】:

所以我想做的是用 C# 创建一个简单的基于文本的 RPG 游戏。在询问玩家他/她想使用什么武器后,如果用户按下了无效的键,我希望能够重新开始循环。

//Runs battle interactive
Console.WriteLine("");
Console.WriteLine("You have encountered a simple guard!  He deals 2 damage per attack and has 1 HP.");
Console.WriteLine("You currently have: " + Program.Inventory);
Console.WriteLine("Choose a weapon!");
var input2 = Console.ReadKey();

//Key checker for items
switch (input2.Key)
{
    case ConsoleKey.D1:
        Console.WriteLine("");
        if (Items.iniFists == true)
        {
            Console.WriteLine("You have attacked with your Fists for 1 DMG!");
        }else
        {
            //this will never run, just a placeholder
            Console.WriteLine("You Don't have your fists!");
            switch (input2.Key)
{
    case ConsoleKey.D1:
        Console.WriteLine("");
        if (Items.iniFists == true)
        {
            Console.WriteLine("You have attacked with your Fists for 1 DMG!");
        }else
        {
            //this will never run, just a placeholder
            Console.WriteLine("You Don't have your fists!");
        }
        break;
    case ConsoleKey.D2:
        Console.WriteLine("");
        if (Items.iniLongsword == true)
        {
        Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!");
        }else
        {
            Console.WriteLine("You don't have a longsword!");
        }
        break;
    case ConsoleKey.D3:
        Console.WriteLine("");
        if (Items.iniBow == true)
        {
            Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!");
        }
        else
        {
            Console.WriteLine("You don't have a Bow!");
        }
        break;
    case ConsoleKey.D4:
        Console.WriteLine("");
        if (Items.iniLightstaff == true)
        {
            Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!");
        }
        else
        {
            Console.WriteLine("You don't have a Lightstaff!");
        }
        break;
    case ConsoleKey.D5:
        Console.WriteLine("");
        Console.WriteLine("You can't attack with an Apple!");
        break;
    case ConsoleKey.D6:
        Console.WriteLine("");
        Console.WriteLine("You can't attack with a Golden Key!");
        break;
    case ConsoleKey.D7:
        Console.WriteLine("");
        Console.WriteLine("You can't attack with a Steak!");
        break;
}
        }
        break;
    case ConsoleKey.D2:
        Console.WriteLine("");
        if (Items.iniLongsword == true)
        {
        Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!");
        }else
        {
            Console.WriteLine("You don't have a longsword!");
        }
        break;
    case ConsoleKey.D3:
        Console.WriteLine("");
        if (Items.iniBow == true)
        {
            Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!");
        }
        else
        {
            Console.WriteLine("You don't have a Bow!");
        }
        break;
    case ConsoleKey.D4:
        Console.WriteLine("");
        if (Items.iniLightstaff == true)
        {
            Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!");
        }
        else
        {
            Console.WriteLine("You don't have a Lightstaff!");
        }
        break;
    case ConsoleKey.D5:
        Console.WriteLine("");
        Console.WriteLine("You can't attack with an Apple!");
        break;
    case ConsoleKey.D6:
        Console.WriteLine("");
        Console.WriteLine("You can't attack with a Golden Key!");
        break;
    case ConsoleKey.D7:
        Console.WriteLine("");
        Console.WriteLine("You can't attack with a Steak!");
        break;
}

例如,如果玩家按下 7 键,游戏会告诉他们不能用牛排进行攻击。告诉玩家之后,如何让玩家选择其他物品?

【问题讨论】:

  • 在我看来,你发布的代码太多了。
  • @AgentMcBride:你可以使用 wile(true) 循环,看我的回答。
  • @AgentMcBride 将您的 UI 功能与您的程序的功能混合往往会使您的代码更难维护。一旦您的代码正常工作,我建议您向 codereview.stackexchange.com 询问有关使其更清洁和更易于维护的建议。

标签: c# loops key


【解决方案1】:

这是你的代码包裹在一个循环中:

bool correct = false;
do
{
    Console.WriteLine("");
    Console.WriteLine("You have encountered a simple guard!  He deals 2 damage per attack and has 1 HP.");
    Console.WriteLine("You currently have: " + Program.Inventory);
    Console.WriteLine("Choose a weapon!");
    var input2 = Console.ReadKey();

    correct = true;
    //Key checker for items
    switch (input2.Key)
    {
        case ConsoleKey.D1:
            Console.WriteLine("");
            if (Items.iniFists == true)
            {
                Console.WriteLine("You have attacked with your Fists for 1 DMG!");
            }
            else
            {
                //this will never run, just a placeholder
                Console.WriteLine("You Don't have your fists!");
                switch (input2.Key)
                {
                    case ConsoleKey.D1:
                        Console.WriteLine("");
                        if (Items.iniFists == true)
                        {
                            Console.WriteLine("You have attacked with your Fists for 1 DMG!");
                        }
                        else
                        {
                            //this will never run, just a placeholder
                            Console.WriteLine("You Don't have your fists!");
                        }
                        break;
                    case ConsoleKey.D2:
                        Console.WriteLine("");
                        if (Items.iniLongsword == true)
                        {
                            Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!");
                        }
                        else
                        {
                            Console.WriteLine("You don't have a longsword!");
                        }
                        break;
                    case ConsoleKey.D3:
                        Console.WriteLine("");
                        if (Items.iniBow == true)
                        {
                            Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!");
                        }
                        else
                        {
                            Console.WriteLine("You don't have a Bow!");
                        }
                        break;
                    case ConsoleKey.D4:
                        Console.WriteLine("");
                        if (Items.iniLightstaff == true)
                        {
                            Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!");
                        }
                        else
                        {
                            Console.WriteLine("You don't have a Lightstaff!");
                        }
                        break;
                    case ConsoleKey.D5:
                        Console.WriteLine("");
                        Console.WriteLine("You can't attack with an Apple!");
                        break;
                    case ConsoleKey.D6:
                        Console.WriteLine("");
                        Console.WriteLine("You can't attack with a Golden Key!");
                        break;
                    case ConsoleKey.D7:
                        Console.WriteLine("");
                        Console.WriteLine("You can't attack with a Steak!");
                        break;
                }
            }
            break;
        case ConsoleKey.D2:
            Console.WriteLine("");
            if (Items.iniLongsword == true)
            {
                Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!");
            }
            else
            {
                Console.WriteLine("You don't have a longsword!");
            }
            break;
        case ConsoleKey.D3:
            Console.WriteLine("");
            if (Items.iniBow == true)
            {
                Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!");
            }
            else
            {
                Console.WriteLine("You don't have a Bow!");
            }
            break;
        case ConsoleKey.D4:
            Console.WriteLine("");
            if (Items.iniLightstaff == true)
            {
                Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!");
            }
            else
            {
                Console.WriteLine("You don't have a Lightstaff!");
            }
            break;
        case ConsoleKey.D5:
            Console.WriteLine("");
            Console.WriteLine("You can't attack with an Apple!");
            break;
        case ConsoleKey.D6:
            Console.WriteLine("");
            Console.WriteLine("You can't attack with a Golden Key!");
            break;
        case ConsoleKey.D7:
            Console.WriteLine("");
            Console.WriteLine("You can't attack with a Steak!");
            break;
        default:
            correct = false;
            break;
    }
}
while (!correct);

【讨论】:

  • 对不起,我对编程有点陌生,这到底在哪里?
  • @AgentMcBride 将该循环包裹在您的 switch 语句周围。
  • @sysexpand 所以在我把它放在我的代码中之后,它只会说当我输入错误的数字时选择武器,没有别的。
  • 在默认部分添加您认为适合错误键的任何代码,例如打印出消息。循环将允许用户再次选择。
【解决方案2】:

示例代码:

string key;
do {

   key = Console.Read(); 
   if(key == ..) {
   }
   else if(key == ...) {
   }
   else if(..) {
   }

}while(key == WRONG_KEY);

【讨论】:

    【解决方案3】:

    您需要打印出“请按另一个键”,然后添加一个新的输入变量和 switch 语句。这样用户可以按两次键7。当用户第一次按下按钮时,第一个开关将用于做任何你想做的事情。第二个 switch 语句将控制用户再次按键时发生的情况。

               var input2 = Console.ReadKey();
    
                //Key checker for items
                switch (input2.Key)
                {
                   //CODE HERE (PRINT PLEASE PRESS ANOTHER KEY)
                }
    
               var input3 = Console.ReadKey();
                switch (input3.Key)
                {
                   //CODE HERE 
                }
    

    【讨论】:

      【解决方案4】:

      您可以使用while(true)解决问题。

      如果您使用while(true),它将迭代infinite 次,但如果一旦用户输入无效输入,您应该确保quit 循环。

      如果用户输入了无效的输入然后没有 switch case mathces,那么它进入default 块,在默认块中你可以使用return 语句退出循环。

      示例代码:

          Console.WriteLine("Please enter key 1-5");
          int key =Convert.Int32(Console.ReadLine());
      
          while (true)
          {
              switch (key)
              {
                  case 1: Console.WriteLine("ur option "+key);
                      break;
                  case 2: Console.WriteLine("ur option " + key);
                      break;
                  case 3: Console.WriteLine("ur option " + key);
                      break;
                  case 4: Console.WriteLine("ur option " + key);
                      break;
                  case 5: Console.WriteLine("ur option " + key);
                      break;
                  default:
                      Console.WriteLine("invalid choice ,quitting the loop");
                      return;
      
              }
              Console.WriteLine("Please enter key 1-5");
               key = Console.ReadLine();
          }
          Console.WriteLine("outside loop");
      

      完整代码:

                  Console.WriteLine("");
                  Console.WriteLine("You have encountered a simple guard!  He deals 2 damage per attack and has 1 HP.");
                  Console.WriteLine("You currently have: " + Program.Inventory);
                  Console.WriteLine("Choose a weapon!");
                  var input2 = Console.ReadKey();
      
                  //Key checker for items
                  while(true)
                  {
                  switch (input2.Key)
                  {
                      case ConsoleKey.D1:
                          Console.WriteLine("");
                          if (Items.iniFists == true)
                          {
                              Console.WriteLine("You have attacked with your Fists for 1 DMG!");
                          }else
                          {
                              //this will never run, just a placeholder
                              Console.WriteLine("You Don't have your fists!");
                              switch (input2.Key)
                  {
                      case ConsoleKey.D1:
                          Console.WriteLine("");
                          if (Items.iniFists == true)
                          {
                              Console.WriteLine("You have attacked with your Fists for 1 DMG!");
                          }else
                          {
                              //this will never run, just a placeholder
                              Console.WriteLine("You Don't have your fists!");
                          }
                          break;
                      case ConsoleKey.D2:
                          Console.WriteLine("");
                          if (Items.iniLongsword == true)
                          {
                          Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!");
                          }else
                          {
                              Console.WriteLine("You don't have a longsword!");
                          }
                          break;
                      case ConsoleKey.D3:
                          Console.WriteLine("");
                          if (Items.iniBow == true)
                          {
                              Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!");
                          }
                          else
                          {
                              Console.WriteLine("You don't have a Bow!");
                          }
                          break;
                      case ConsoleKey.D4:
                          Console.WriteLine("");
                          if (Items.iniLightstaff == true)
                          {
                              Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!");
                          }
                          else
                          {
                              Console.WriteLine("You don't have a Lightstaff!");
                          }
                          break;
                      case ConsoleKey.D5:
                          Console.WriteLine("");
                          Console.WriteLine("You can't attack with an Apple!");
                          break;
                      case ConsoleKey.D6:
                          Console.WriteLine("");
                          Console.WriteLine("You can't attack with a Golden Key!");
                          break;
                      case ConsoleKey.D7:
                          Console.WriteLine("");
                          Console.WriteLine("You can't attack with a Steak!");
                          break;
                  }
                          }
                          break;
                      case ConsoleKey.D2:
                          Console.WriteLine("");
                          if (Items.iniLongsword == true)
                          {
                          Console.WriteLine("You have chosen to attack with the Longsword for 2 DMG!");
                          }else
                          {
                              Console.WriteLine("You don't have a longsword!");
                          }
                          break;
                      case ConsoleKey.D3:
                          Console.WriteLine("");
                          if (Items.iniBow == true)
                          {
                              Console.WriteLine("You have chosen to attack with the Bow for 3 DMG!");
                          }
                          else
                          {
                              Console.WriteLine("You don't have a Bow!");
                          }
                          break;
                      case ConsoleKey.D4:
                          Console.WriteLine("");
                          if (Items.iniLightstaff == true)
                          {
                              Console.WriteLine("You have chosen to attack with the Lightstaff for 4 DMG!");
                          }
                          else
                          {
                              Console.WriteLine("You don't have a Lightstaff!");
                          }
                          break;
                      case ConsoleKey.D5:
                          Console.WriteLine("");
                          Console.WriteLine("You can't attack with an Apple!");
                          break;
                      case ConsoleKey.D6:
                          Console.WriteLine("");
                          Console.WriteLine("You can't attack with a Golden Key!");
                          break;
                      case ConsoleKey.D7:
                          Console.WriteLine("");
                          Console.WriteLine("You can't attack with a Steak!");
                          break;
                      default:  Console.WriteLine("Invalid Option Qutting loop");
                               return;
      
                  }
                  Console.WriteLine("Choose a weapon!");
                  var input2 = Console.ReadKey();
                  }
      

      【讨论】:

        猜你喜欢
        • 2019-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-04
        • 1970-01-01
        相关资源
        最近更新 更多