这里是一个完整的初学者。
我从C ++开始,通常是编程,所以,我试图为文本RPG格斗游戏做一个粗糙的菜单,以练习条件训练。
但是,这些值不会自我更新。这是代码:
int main() {
cout << "Wild Ogre attacked!" << endl << endl;
int ogreHP = 350;
int HP = 100;
int Stamina = 100;
int Magicka = 100;
while (ogreHP > 1) {
cout << "HP: " << HP << endl;
cout << "Stamina: " << Stamina << endl;
cout << "Magicka: " << Magicka << endl << endl;
cout << "Ogre HP: " << ogreHP << endl << endl;
cout << "What are you going to do? " << endl;
cout << "1.\tAttack." << endl;
cout << "2.\tMagicka." << endl;
cout << "3.\tTactics." << endl;
cout << "4.\tBag." << endl;
cout << "5.\tRun." << endl;
int menu_input;
cin >> menu_input;
switch (menu_input) {
case 1: {
cout << "1.\tLight Attack." << endl;
cout << "\t\tDeals 5 points of damage. No Stamina cost." << endl << endl;
cout << "2.\tHeavy Attack" << endl;
cout << "\t\tDeals 20 points of damage. Stamina Cost of 10 points." << endl << endl;
int att_input;
cin >> att_input;
switch (att_input) {
case 1: {
cout << "You dealt 5 points of damage!" << endl << endl;
ogreHP= - 5;
break;
}
case 2: {
cout << "You dealt 20 points of damage!" << endl << endl;
Stamina= - 10;
ogreHP= - 20;
break;
}
谢谢!
答案 0 :(得分:0)
编写此语法时:
Stamina = - 10;
ogreHP = - 20;
您实际上将-10
和-20
分别分配给Stamina
和ogreHP
,在这里它们不称为“减少”。相反,如果您可以编写以下内容:
Stamina -= 10; // Stamina = Stamina - 10; -> previous - 10 = now
ogreHP -= 20; // ogreHP = ogreHP - 20; -> previous - 20 = now
// ^^ is called 'assignment operator'
问题将得到解决。与案例1中的代码相同。