c – 如何将引用变量增加1

前端之家收集整理的这篇文章主要介绍了c – 如何将引用变量增加1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是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;

猜你在找的C&C++相关文章