无法弄清楚出了什么问题,函数等之后的类中没有正确的变量值
Can't figure out what's wrong not a right variable value in class after functions and so on
>Heyo!我突然对突然C++编码产生了兴趣,所以我开始研究,现在我决定写一个小的基于文本的口袋妖怪游戏!但是我无法弄清楚我的代码中出了什么问题,也许我只是在做一个我不知道的新手错误?因为我什至不知道我应该在谷歌中搜索什么。这是代码:
class Charmander {
public:
int lvl=0;
int xp=0;
int str=15+3*lvl;
int agi=15+3*lvl;
int hp=30+6*lvl;
int energy=25+5*lvl;
int cen=0;
int nxp=1000;
}cha;
class Squirtle {
public:
int lvl=0;
int xp=0;
int str=15+3*lvl;
int agi=15+3*lvl;
int hp=30+6*lvl;
int energy=25+5*lvl;
int cen=0;
int nxp=1000;
}squ;
class Pikachu {
public:
int lvl;
int xp;
int str=10+2*lvl;
int agi=25+5*lvl;
int hp=25+5*lvl;
int energy=25+5*lvl;
int cen=0;
int nxp=1000;
}pik;
class Bulbasaur {
public:
int lvl=0;
int xp=0;
int str=20+4*lvl;
int agi=10+2*lvl;
int hp=35+7*lvl;
int energy=25+5*lvl;
int cen=0;
int nxp=1000;
}bul;
class CurrentPokemon {
public:
int lvl=0;
int fl=0;
int xp=0;
int str=0;
int agi=0;
int hp=0;
int energy=0;
int cen=0;
int nxp=1000;
string tag;
string name;
}curr;
int level () {
int lvlup;
lvlup = curr.xp/curr.nxp;
while (lvlup >= 1) {
lvlup = curr.xp/curr.nxp;
if (lvlup >= 1) {
curr.lvl++;
curr.xp=curr.xp-curr.nxp;
curr.nxp=curr.nxp*1.5;
cout << "You reached level " << curr.lvl << " !" << endl;
}
else {
break;
}
}
}
int update () {
if (curr.lvl >> curr.fl) {
if (curr.name == "Pikachu") {
pik.nxp=curr.nxp;
pik.xp=curr.xp;
pik.lvl=curr.lvl;
pik.cen=curr.cen;
curr.str=pik.str;
curr.agi=pik.agi;
curr.hp=pik.hp;
curr.energy=pik.energy;
}
}
}
int main() {
string a,c1,c2,c3,c4,g;
int f;
cout << "You are starting your journey as a pokemon master!" << endl;
Sleep (200);
cout << "You have to choose your companion for the advantures you will experience!" << endl;
Sleep (200);
tryagain:
cout << "c for Charmander, s for Squirtle, p for Pikachu, b for Bulbasaur!" << endl;
cin >> c1;
if (c1 == "c") {
a = "Charmander";
cha.str = cha.str + 10;
cha.agi = cha.agi + 7;
cha.hp = cha.hp + 20;
cha.energy = cha.hp + 20;
cha.cen = cha.cen + 20;
curr.str = cha.str;
curr.agi = cha.agi;
curr.hp = cha.hp;
curr.energy = cha.energy;
curr.cen = cha.cen;
curr.tag = "Fire";
curr.name = "Charmander";
}
else if (c1 == "s") {
a = "Squirtle";
squ.str = squ.str + 12;
squ.agi = squ.agi + 10;
squ.hp = squ.hp + 25;
squ.energy = squ.energy + 20;
squ.cen = squ.cen + 20;
curr.str = squ.str;
curr.agi = squ.agi;
curr.hp = squ.hp;
curr.energy = squ.energy;
curr.cen = squ.cen;
curr.tag = "Water";
curr.name = "Squirtle";
}
else if (c1 == "p") {
a = "Pikachu";
pik.str = pik.str + 8;
pik.agi = pik.agi + 15;
pik.hp = pik.hp + 15;
pik.energy = pik.energy + 20;
pik.cen = pik.cen + 20;
curr.str = pik.str;
curr.agi = pik.agi;
curr.hp = pik.hp;
curr.energy = pik.energy;
curr.cen = pik.cen;
curr.tag = "Eletricity";
curr.name = "Pikachu";
}
else if (c1 == "b") {
a = "Bulbasaur";
bul.str = bul.str + 15;
bul.agi = bul.agi + 2;
bul.hp = bul.hp + 30;
bul.energy = bul.energy + 20;
bul.cen = bul.cen + 20;
curr.str = bul.str;
curr.agi = bul.agi;
curr.hp = bul.hp;
curr.energy = bul.energy;
curr.cen = bul.cen;
curr.tag = "Plant";
curr.name = "Bulbasaur";
}
else {
cout << "Sorry, wrong choice, try again!" << endl;
goto tryagain;
}
cout << "You have chosen " << a << " ,may you be friends forever!" << endl;
Sleep (200);
tryagain2:
cout << "Would you like to pass a training course with your pokemon? (y/n)" << endl;
cin >> c2;
if (c2 == "y") {
curr.xp = curr.xp + 1500;
curr.str = curr.str + 20;
curr.agi = curr.agi + 5;
curr.hp = curr.hp + 5;
curr.energy = curr.energy + 10;
curr.cen = curr.cen - 15;
}
else if (c2 == "n") {
cout << "Very well then." << endl;
}
level();
update();
cin >> f;
if (f==1) {
curr.xp = curr.xp + 600;
}
level();
if (f==2) {
curr.xp = curr.xp + 4600;
}
level();
update();
cout << " lvl " << curr.lvl << " xp " << curr.xp << " str " << curr.str << " agi " << curr.agi << " hp " << curr.hp << " energy " << curr.energy << " Current energy" << curr.cen << " " << curr.tag << endl;
cout << " lvl " << pik.lvl << " xp " << pik.xp << " str " << pik.str << " agi " << pik.agi << " hp " << pik.hp << " energy " << pik.energy << " Current energy" << pik.cen << " " << endl;
return 0;
}
每一步之后,我只对皮卡丘进行了 int 更新 () 我想检查一切是否正确。 所以我的选择是 p -> y -> 2 我想要得到的是:str 10 -> p -> str 18 -> y -> 2 -> (此时级别为 3) -> str 24 ,但我得到的是 str 18、agi 40、hp 40、能量 45、电流能量 5、xp 1350、lvl 3(在两条线上,curr 和 pik)。
如果有人能帮助我解决这个问题,我将不胜感激!祝你有美好的一天,谢谢。
请考虑另一种设计。 也许是这样的:
class Pokemon {
protected:
std::string name;
int lvl;
int xp;
int str;
int agi;
int hp;
int energy;
int cen;
int nxp;
public:
Pokemon(std::string name);
int level ();
int update ();
};
class Charmander : public Pokemon {
public:
Charmander ();
};
Charmander::Charmander (std::string name)
: Pokemon(name)
{
name = name;
lvl=0;
xp=0;
str=15+3*lvl;
agi=15+3*lvl;
hp=30+6*lvl;
energy=25+5*lvl;
cen=0;
nxp=1000;
};
...
将常见功能分解到基类"口袋妖怪"中。
将每个类的功能添加到子类(查曼德、皮卡丘等)中。
你还应该让像"level()"和"update()"这样的函数成为基类的一部分。 如果行为因类而异,则使它们成为"虚拟"函数;例如,如果"Pikachu.update()"与"Charmander.update()"完全不同。
图书推荐:
- 编程 -- 使用C++的原则与实践(第二版)
它并不短 - 但它很好地介绍了使用C++的C++和面向对象的设计策略。
相关文章:
- 使用变量值作为 PlaySound 中的路径
- 通过指针偏移量访问结构变量值
- 变量值在 C++ 中的 glutIdleFunc() 中未递增
- 更新 #ifdef 中的现有变量值?
- 尝试更改 main 函数内的全局变量值时出现编译错误 C++.
- 通过 Rcpp 和 bit64 R 包将最大的int64_t变量值从 C++ 传递到 R
- 如何使用 IRBuilder 更新 LLVM IR 中的全局变量值?
- 更改已在运行的程序的变量值
- 在 Xcode 中使用后对外部变量值进行初始化
- C++整数变量值
- 变量值,在三元运算符之后
- 如何获得对不同类的引用,以便从所述类内部获得变量值
- Java应用程序的JNI-c++变量值
- 如何固定从传感器获得的变量值
- 离开方法时重置变量值
- 打印具有通用特定变量值的类对象
- 在宏函数C++中设置宏变量值
- 为什么每次迭代都会获得相同的变量值? C++
- 如何根据基类对象中的变量值分配派生类中的变量?
- 无法弄清楚出了什么问题,函数等之后的类中没有正确的变量值