【问题标题】:I am not getting the desired output from the code, it seems something is wrong with the while loop我没有从代码中获得所需的输出,while 循环似乎有问题
【发布时间】:2020-01-27 10:59:02
【问题描述】:

1) 在游戏开始时,用户可以选择玩家类型(1、2、3)

2) 每种类型的玩家都有以下属性-

3) 玩家 1 - 最大生命值 = 50,伤害 = 10,治疗 = 20

4) 玩家 2 - 最大生命值 = 75,伤害 = 25,治疗 = 10

5) 玩家 3 - 最大生命值 = 100,伤害 = 75,治疗 = 20

6) 玩家只能恢复到最大生命值(忽略这一点,因为我还没有编写代码

7) 生成玩家后,游戏让玩家受到伤害或得到治疗,如下所示

8) “按 D(承受伤害)或 H(治疗)玩家”

9) 按 D 或 H 可以无限次重复,直到玩家的生命值降至 0 以下

10) 当玩家生命值为 0 或低于 0 时,游戏结束!

11) 现在用户会看到以下选项 -

12) 按 S 重新开始游戏

13) 按 E 结束程序

14) 如果用户按下“S”,整个游戏将重新开始,用户现在可以创建一个新玩家 输入 (1, 2, 3)

15) 如果用户按下“E”,整个程序结束!

问题: ]1 当满足退出 while 循环的条件时,它确实会退出 while 循环,但在按下任何其他数字时会产生“伤害”并且“治愈”不起作用输出为空白

#include <iostream>

using namespace std;

int input;
char input1;
bool gameOver;

class player
{
    public:void p(int Health,int Damage,int Heal)
    {
        do
        {
          cout << "Player Health is" << Health;
          cin >> input1;

          switch(input1)
              {
                  case 'd':
                      Health -= Damage;
                      break;
                  case 'h':
                      Health += Heal;
                      break;
              }

           if(Health == 0 || input1 == 's')
           {
                gameOver = true;
           }

        } while (gameOver == false);
    }
}p1;

int main()
{

    do{
        //system("cls");
        cin >> input;

        if(input == 1)
        {
            p1.p(50,10,20);
        }
        if(input == 2)
        {
            player p2;
            p2.p(75,25,10);
        }
        if(input == 3)
        {
            player p2;
            p2.p(100,75,20);
        }



    } while (input != 5);

    return 0;
}

【问题讨论】:

    标签: c++ function class


    【解决方案1】:

    当您开始下一个玩家游戏循环时,变量 gameOver 不会重置为 false。 试试这样的:

    void p(int Health, int Damage, int Heal)
    {
      gameOver = false;
      do
      {
        // game logic...
      } while (gameOver == false)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多