0

无论 cin 它继续输出 0 的分数。这是为什么?我尝试返回“return 0;” 但还是不行:/

#include "stdafx.h"
#include <iostream>

using namespace std;

// Variables
int enemiesKilled;
const int KILLS = 150;
int score = enemiesKilled * KILLS; 

int main()
{
    cout << "How many enemies did you kill?" << endl;
    cin >> enemiesKilled;
    cout << "Your score: " << score << endl;
    return 0;
}
4

4 回答 4

4

您需要在用户输入他们的输入后执行乘法:

int main()
{
    cout << "How many enemies did you kill?" << endl;
    cin >> enemiesKilled;
    int score = enemiesKilled * KILLS;
    cout << "Your score: " << score << endl;
    return 0;
}

在线查看:ideone

于 2012-12-17T10:48:43.977 回答
1

用户输入数字score ,您需要重新计算。

于 2012-12-17T10:48:43.883 回答
1
cin >> enemiesKilled;
score = enemiesKilled * KILLS; // <-- Put the calculation here!!
cout << "Your score: " << score << endl;
于 2012-12-17T10:50:03.347 回答
1

int enemiesKilled;程序启动时初始化为 0。

int score = enemiesKilled * KILLS;同时计算。因为enemiesKilled是0,所以也是0。

如其他答案所述,您需要score在运行时进行计算。

于 2012-12-17T10:50:06.617 回答