整数不更新C ++中开关函数中的值

Interger not updating value in switch function in C++

有点完全是初学者。

我从 C++ 开始,一般编程,所以,我正在尝试为文本 RPG 战斗游戏做一个粗略的菜单,以练习条件。

但是,这些值不会自行更新。这是代码:

int main() {

    cout << "Wild Ogre attacked!" << endl << endl;
    int ogreHP = 350;
    int HP = 100;
    int Stamina = 100;
    int Magicka = 100;

    while (ogreHP > 1) {
    cout << "HP: " << HP << endl;
    cout << "Stamina: " << Stamina << endl;
    cout << "Magicka: " << Magicka << endl << endl;

    cout << "Ogre HP: " << ogreHP << endl << endl;

    cout << "What are you going to do? " << endl;
    cout << "1.\tAttack." << endl;
    cout << "2.\tMagicka." << endl;
    cout << "3.\tTactics." << endl;
    cout << "4.\tBag." << endl;
    cout << "5.\tRun." << endl;
 
    int menu_input;
    cin >> menu_input;

    switch (menu_input) {
        case 1: {
            
            cout << "1.\tLight Attack." << endl;
            cout << "\t\tDeals 5 points of damage. No Stamina cost." << endl << endl;
            cout << "2.\tHeavy Attack" << endl;
            cout << "\t\tDeals 20 points of damage. Stamina Cost of 10 points." << endl << endl;
            
            int att_input;
            cin >> att_input;
            
            switch (att_input) {

                case 1: {

                    cout << "You dealt 5 points of damage!" << endl << endl;
                    ogreHP= - 5;
            
                    break;
                }
                case 2: {

                    cout << "You dealt 20 points of damage!" << endl << endl;
                    Stamina= - 10;
                    ogreHP= - 20;

                    break;
                }

提前致谢!

当你写这个语法时:

Stamina = - 10;
ogreHP = - 20;

你实际上把-10-20分别赋给了StaminaogreHP,这里不叫'reducing'。而不是那样,如果您可以编写以下内容:

Stamina -= 10; // Stamina = Stamina - 10; -> previous - 10 = now
ogreHP -= 20;  // ogreHP = ogreHP - 20;   -> previous - 20 = now
//     ^^ is called 'assignment operator'

问题就解决了。与您在代码中的案例 1 中所做的相同。