我最近才接触到 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。我相信这是因为四舍五入?如果不让我知道。
如果是这样,我该如何解决这个问题?我认为答案就在这里的某个地方,但我不知道到底要搜索什么,因为我不知道到底是什么问题。据我所知, float 可以帮助我吗?我不明白什么是 float ,因为这是我编码的第一件事。
最佳答案
您已将 BKDMG 定义为 int
类型,这意味着它只能容纳整数。所以,是的,如果你给它分配一个实数值,它会将它向下舍入到下一个整数值。对于此示例,使用 double
或 float
可能就足够了。
你的条件也不对:
if ( BK = 1 ) {
BKDMG = 1.6;
}
因为'BK = 1'是一个赋值,它总是将BK的值设置为1。上面的代码片段应该是:
if ( BK == 1 ) {
BKDMG = 1.6;
}
关于c++ - 等式中不考虑小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23493690/