我最近才进入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.我相信这是因为四舍五入?如果没有让我知道。
如果是,我该如何解决这个问题?我认为答案就在这里,但我不知道究竟要搜索什么,因为我不确切知道问题是什么。据我所知,漂浮可以帮助我吗?我不明白浮子是什么,因为这是我编码的第一件事。
答案 0 :(得分:1)
您已将BKDMG定义为int
类型,这意味着它只能保存整数。所以,是的,如果你为它指定一个实数值,它会将它向下舍入到下一个整数值。使用double
或float
可能足以满足此示例。
你的条件也错了:
if ( BK = 1 ) {
BKDMG = 1.6;
}
因为'BK = 1'是一个赋值,它总是将BK的值设置为1.上面的代码片段应为:
if ( BK == 1 ) {
BKDMG = 1.6;
}
答案 1 :(得分:1)
if ( BK = 1 )
经典(初学者)错误。你没有比较,你正在分配。
应该是
if ( BK == 1 )
甚至更好,
if ( 1 == BK)
如果你把常数放在第一位,就不可能出错。