#include <iostream> #include <string> using namespace std; class Player {private: string player_name; double player_number; int player_level; int experience;public: Player(string name, double number, int level, int exp) { player_name = name; player_number = number; player_level = level; experience = exp; } void gainExperience(int exp) { experience += exp; if (experience > int player_level* 100,player_level++) { ; cout << "恭喜升级,当前等级为" << player_level << endl; } } void show() { cout << "姓名:" << player_name << endl; cout << "序号:" << player_number << endl; cout << "等级:" << player_level << endl; cout << "经验:" << experience << endl; }}; int main() { Player n("368", 64738, "1", 0); n.gainExperience(120); n.show(); return 0;}改正该程序,
时间: 2024-01-22 11:02:01 浏览: 24
有几个错误需要修改:
1. 构造函数中,第三个参数应该是 int 类型,而不是 string 类型。
2. 判断升级的条件应该是当前经验值大于等于当前等级乘以 100,而不是大于。
3. 升级后应该将当前经验值重置为 0。
以下是修改后的程序:
```
#include <iostream>
#include <string>
using namespace std;
class Player {
private:
string player_name;
double player_number;
int player_level;
int experience;
public:
Player(string name, double number, int level, int exp) {
player_name = name;
player_number = number;
player_level = level;
experience = exp;
}
void gainExperience(int exp) {
experience += exp;
if (experience >= player_level * 100) {
player_level++;
experience = 0;
cout << "恭喜升级,当前等级为" << player_level << endl;
}
}
void show() {
cout << "姓名:" << player_name << endl;
cout << "序号:" << player_number << endl;
cout << "等级:" << player_level << endl;
cout << "经验:" << experience << endl;
}
};
int main() {
Player n("368", 64738, 1, 0);
n.gainExperience(120);
n.show();
return 0;
}
```
输出结果为:
```
恭喜升级,当前等级为2
姓名:368
序号:64738
等级:2
经验:0
```
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)