我最近才刚接触C ++,并且计划主要从事游戏。自然,我决定玩转,看看我能做什么。

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main ()
{
    int HIT, DMG, HRT, ATK, DEF, EVS, EVS1, CRT, CRT1, CRT2, CRTMUL, CRTMUL1, BK, BKDMG;

    srand( time(0));

    HIT=rand()%100+1;

    cout<<"Please enter thier Evasion: ";
    cin >> EVS;

    EVS1 = 100 - EVS;

    if ( HIT < EVS1 ) {
        cout<<"Hit!";
        cout << '\n';
    }

    else if ( HIT > EVS1 ) {
        cout<<"Miss!";
        return 0;
    }

    cout<<"Please enter your Damage: ";
    cin >> DMG;

    cout<<"Please enter your Attack: ";
    cin >> ATK;

    cout<<"Please enter thier Defence: ";
    cin >> DEF;

    cout<<"Please enter your Crit Chance: ";
    cin >> CRT;

    cout<<"Please enter your Crit Multiplier: ";
    cin >> CRTMUL1;


    CRT1=rand()%100+1;

    CRT2 = 100 - CRT;

    if ( CRT1 < CRT2 ) {
        cout<<"You didnt crit.";
        cout << '\n';
    CRTMUL = 1;
    }

    else if ( CRT1 > CRT2 ) {
        cout<<"Crit!";
        cout << '\n';
        CRTMUL = CRTMUL1;
    }


    // no matter what you input here,...
    cout<<"From where did you hit them? ";
    cout << '\n';
    cout<<"(1 for from the back, 2 for from the side, 3 for from the front).";
    cout << '\n';
    cin >> BK;
    // ...this area...
    if ( BK = 1 ) {
        BKDMG = 1.6;
    }

    else if ( BK = 2 ) {
        BKDMG = 1.3;
    }

    else if ( BK = 3 ) {
        BKDMG = 1;
    }
    // ... to this area wont work, in the equation below BKDMG is allways 1
    HRT =  ((((((ATK/5)/100)+1)*(DMG))-(((((ATK/5)/100)+1)*(DMG))/100)*(DEF/5))*BKDMG)*CRTMUL;

    cout<<"You hit for ";
    cout<<HRT;
    cout<<" damage!";

    return 0;
}


正如您在代码中看到的那样,无论您为BK输入什么,BKDMG似乎都将以1表示。我相信这是因为舍入吗?如果没有让我知道。

如果是这样,我该如何解决此问题?我认为答案就在这里,但是我不知道确切要搜索什么,因为我不知道到底是什么问题。据我所知,浮动可以帮助我吗?我不明白什么是浮点数,因为这是我编写的第一件事。

最佳答案

您已将BKDMG定义为int类型,这意味着它只能容纳整数。因此,是的,如果您为其分配一个实数值,它将把它四舍五入到下一个整数值。对于此示例,使用doublefloat可能就足够了。

您的条件也错了:

if ( BK = 1 ) {
    BKDMG = 1.6;
}


因为'BK = 1'是一个赋值,所以它总是将BK的值设置为1。上面的代码段应为:

if ( BK == 1 ) {
    BKDMG = 1.6;
}

关于c++ - 等式中未考虑小数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23493690/

10-13 00:04