c++ - 整数不更新 C++ 中 switch 函数中的值
问题描述
有点像一个完整的初学者。
我从 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;
}
提前致谢!
解决方案
当您编写此语法时:
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 中所做的相同的事情。
推荐阅读
- r - 如何将Shapefiles中的边界框覆盖到ggplot中?
- typescript - 具有多个长度的元组的重载函数的 Typescript 实现签名
- c# - 正则表达式匹配重复文本除以哈希字符,可以在多行字符串中重复多次
- c++ - Qt3D:如何将 Scene2D 缩放为与窗口相同的大小(按像素计算)?
- javascript - JS动画不会让div变大
- c++ - 如何使用标题单元
- c - 为什么枚举常量而不是结构声明中的成员具有文件范围?
- python-pptx - AttributeError:“SlidePlaceholder”对象没有属性“insert_picture”
- r - 将字符串日期转换为日期数据类型
- html - 为什么 Firefox 83 默认为多选选项列表中的选项添加明显可见的填充?