在while循环中使用数组

问题描述

我一直在努力,这是基于回合制战斗系统的测试程序。除if语句外,其他所有程序都运行良好。再次选择后,它应该使用下一个数字,但是它始终卡在第一个数字上。如果您有更有效的方法,将不胜感激。

#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
    int item;
    int potion[] = { 20,15,10,5 };
    int p = 0;
    int battle;
    int health = 100;
    int attack = 25;
    int ehealth;
    float eattack = 20;
    int magic = 50;
    ehealth = 100;
    cout << "1 attack,2 attack with magic,3 Guard attack,4 Use items" << endl;
    while (ehealth > 0) {
        cin >> battle;
        switch (battle) {
        case 1: {
            cout << "You did " << attack << " damage!\n" << endl;
            ehealth = ehealth - attack;
            break;
        }
        case 2: {
            cout << "You used magic doing " << magic << " damage!\n" << endl;
            ehealth = ehealth - magic;
            break;
        }
        case 3: {
            cout << "You guard against the the attack!\n" << endl;
            health = health - (eattack / 10);
            break;
        }
        case 4: {
            cout << "Pick an item.\n 1. potion\n" << endl;
            cin >> item;
            if (item == 1) {
                cout << "You recovered " << potion[p] << " Hp" << endl;
                health = health + potion[p];
            }
            break;
        }
        }
        cout << "The enemy attacks!\n" << endl;
        health = health - eattack;
        cout << "Enemy Health: " << ehealth << endl;
        cout << "Your Health: " << health << endl;
    }
    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)