【问题标题】:How to receive key input without requiring ENTER? [duplicate]如何在不需要 ENTER 的情况下接收键输入? [复制]
【发布时间】:2023-03-16 13:01:01
【问题描述】:
i=0;
while(i<=20)
{
    nullchoice:
    system("CLS");
    //Advanced Information
    cout << "Your Statistics: " << endl;
    cout << endl << endl;
    cout << "   1  Strength: " << str << endl;
    cout << endl;
    cout << "   2  Vitality: " << vit << endl;
    cout << endl;
    cout << "   3  Agility: " << agi << endl;
    cout << endl;
    cout << "   4  Thaumaturgy: " << tha << endl;
    cout << endl << endl;
    cout << "Points Remaining: " << lvlskills << endl;
    cout << endl;
    cout << "Enter the number of the skill you wish to increase: ";

    //Applying points to skill attributes

    if(i<=19)
    {
          cin >> input;

          if(input==1)
                str+=1;
          else if(input==2)
               vit+=1;
          else if(input==3)
               agi+=1;
          else if(input==4)
               tha+=1;
          else
               goto nullchoice;

         lvlskills-=1; 
    }    
    i++;
    cout << endl << endl;                     
}

所以本质上,我正在用 C++ 创建一个游戏,一个基于文本的 RPG。这是相当基本的,具有您可能期望从此类游戏中获得的统计数据(力量、活力等)。一开始,如此处所示,玩家可以将一些点数分配给他们选择的技能。

这就是问题所在。现在,玩家必须输入一个数字(1、2、3 或 4。如果不是这些数字,它将进入空选项),然后按 ENTER。让播放器这样做很笨拙,而且完全是错误的,所以有什么简单的方法可以让我编写代码,让他们必须按下数字?

我想我会在接下来的比赛中大量使用它。感谢阅读!

【问题讨论】:

  • 一个相当便携的解决方案是使用 Curses 库。
  • 你想要这个用于什么操作系统?
  • 这适用于 Microsoft Windows
  • 不,这不是重复的线程。其他内容涵盖如何接收按钮输入(getch、cin.get、pause >> nul 等)。所以如果我按“S”它会继续前进,如果我按“R”它会做同样的事情。我需要的是一个条件语句,以便程序可以区分“R”和“S”。
  • 其他问题的答案解决了您的键盘输入问题。您还必须使用自己的大脑,以按照您需要的方式使用收集到的输入。

标签: c++


【解决方案1】:

您只需要存储输入并将其与所需的情况进行比较。考虑一下...

do
{
    system("CLS");
    //Advanced Information
    cout << "Your Statistics: " << endl;
    cout << endl << endl;
    cout << "   1  Strength: " << str << endl;
    cout << endl;
    cout << "   2  Vitality: " << vit << endl;
    cout << endl;
    cout << "   3  Agility: " << agi << endl;
    cout << endl;
    cout << "   4  Thaumaturgy: " << tha << endl;
    cout << endl << endl;
    cout << "Points Remaining: " << lvlskills << endl;
    cout << endl;
    cout << "Enter the number of the skill you wish to increase: ";

    //Applying points to skill attributes
          char input;
          switch(input=getch()){
              case '1':
                str+=1;
                lvlskills-=1;
                break;
          case '2':
               vit+=1;
               lvlskills-=1;
               break;
          case '3':
               agi+=1;
               lvlskills-=1;
               break;
          case '4':
               tha+=1;
               lvlskills-=1;
         } 
    cout << endl << endl;                     
} while(lvlskills>0);

避免在您的代码中使用goto,而您可以通过其他选项来解决它。这是一个很好的做法..

【讨论】:

  • 这仍然需要用户回车。
  • 不..它没有..它已经过测试...
  • 像魅力一样工作。我现在明白这是如何工作的。谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2017-07-27
相关资源
最近更新 更多