我是c的新手,我正在尝试从50开始增加汽车,但如果你的损失比油耗更大,那么只增加1.我希望汽车在下一次循环时保持其价值.我希望这是有道理的.
int Power (int &car); int main(){ int car = 50; // ... // ... // ... int carDamage = 0; int yourDamage = 0; // pick a random number between 1 to 50 yourDamage = 0 + rand() % (50 - 0 + 1); carDamage = 0 + rand() % (50 - 0 + 1); cout << "You hit the car and cause damage of: " << carDamage << endl; cout << "The car hits you and causes damage of: " << yourDamage << endl; cout << endl; if(carDamage < yourDamage) { cout << "You win" << endl; cout << "You gain strength." << endl; cout << endl; int car = car + 1; } else {
解决方法
你正在声明一个新变量遮蔽原始变量.
更改
int car = car + 1;
至
car = car + 1;