- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我试图找出 while 循环和大多数 if 语句,所以我尝试制作这个小游戏,其中你有一定的生命值,怪物也有,只要你们都有超过 0 的生命值,循环就会运行,所以每个循环怪物都会对你造成 50 点伤害,但是使用 scanf 和 if 语句可以通过插入“2”来改变情况,你会减少你所受到的伤害,通过使用“3”你会增加你的生命值并使用“1"你对怪物造成了伤害,但由于某种原因,负责这些事件的 IF 语句似乎不起作用
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int monsterhealth =100;
int health =100;
int damage =50;
while(health>0&&monsterhealth>0){
int action [10];
printf("player has %d health \n",health);
printf("monster has %d health\n",monsterhealth);
printf("act:attack[1]\ndefend[2]\npotion[3]\n");
scanf ("%d",action);
if(action==1){(monsterhealth==monsterhealth-40);}
else if(action==2){(damage==damage-30);}
else (action==3);{(health==health+100);};
health=health-damage;
}
}
最佳答案
关于:
int action [10];
如果您只需要一个操作,请声明一个简单的对象,而不是数组:
int action;
当您进行更改时,您将需要更改:
scanf ("%d",action);
至:
scanf("%d", &action);
将action
的地址传递给scanf
。 scanf
需要知道 action
的地址,以便对其进行更改。如果您使用数组是因为 scanf
似乎不适用于简单对象,那么请不要这样做。数组可以解决您在使用 scanf
时可能遇到的问题,因为它们在 C 中具有某些行为,但这不是正确的解决方案。
关于:
while(health>0&&monsterhealth>0){
不要将代码挤在一起。写得容易阅读,并且间距能够传达含义:
while (health > 0 && monsterhealth > 0) {
关于:
(monsterhealth==monsterhealth-40);
该语句没有任何效果,因为 ==
只是比较两个事物。该语句不会更改任何对象(变量)的值或具有任何其他可观察到的效果。它说“将 monsterhealth
与 monsterhealth-40
进行比较,然后对比较结果不执行任何操作。”这里你想要的是赋值运算符 =
而不是 ==
,它表示“在这个东西中放入一个新值”:
monsterhealth = monsterhealth-40;
以类似方式更改要分配新值的其他语句。
关于:
else (action==3);{(health==health+100);};
多个if
-else
语句的形式为:
if (condition)
statement;
else if (condition)
statement;
else
statement;
请注意,它应该以else语句
结束,而不是else(条件)语句
。使用上面的行结构和缩进。 (有时,可以将带有单个 if
的非常短的语句放在同一行,但除非您更有经验并且具有良好的风格感,否则不要这样做。这大约需要三十年左右的时间开发。)您想要的代码是:
if (action == 1)
monsterhealth = monsterhealth - 40;
else if (action == 2)
damage = damage - 30;
else
health = health + 100;
有些人可能会用有关其所涵盖案例的信息来装饰else
,例如:
if (action == 1)
monsterhealth = monsterhealth - 40;
else if (action == 2)
damage = damage - 30;
else /* action == 3 */
health = health + 100;
有些人主张将所有附加到 if
和 else
语句的语句放在大括号中,以避免某些编辑错误。对于单个语句,C语言不需要它。
C 有另一种形式的语句,旨在处理您在这些 if
语句中测试的特定条件,即 switch
语句,但我认为您刚刚习惯这些语句,稍后将了解 switch
。
关于c - 警告 : statement with no effect [-Wunused-value] with if statements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57429254/
我正在学校作业中实现单例设计模式,这是我的类头文件: class Scheduler { public: static Scheduler * instance(); ~Schedul
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
最近我开始用C++编程了(我是Java出身的,有点零钱哈哈)。在 Windows 下一切正常。问题是我切换到 Linux,这就是我遇到编译器问题的地方。通常当你声明一个变量但没有被使用时,编译器会显示
我是 c++ 新手。 我想忽略警告 -Wunused-result 我猜是因为 -Wall 标志而弹出的。 我在网上搜索了一下,发现这是我可以通过声明一个pragma 来忽略它。我对 pragma 了
我有以下代码,在使用 gcc-4.6 编译时我收到警告: warning: variable ‘status’ set but not used [-Wunused-but-set-variable]
我将Wunused-parameter用于我的项目,但我想让它忽略3rd Party header (特定目录中的 header )。有什么办法可以用cmake设置它吗? 我目前正在像这样设置编译器选
我使用的是 GCC 版本 8.2在几段代码中,我使用了小函数。我对每一项功能都进行了测试(即 Unity 框架测试)。测试被定义为 #define 宏,测试非常具体的事情。例如,如果一个数字是正数
以下代码生成一个警告,指出未使用 temp(这是真的): #include int f() { return 5; } int main() { if(const int& temp = f
我只是花了一些时间来找出为什么可变集没有正确地与自身相交与另一组,使用 [someMutableSet intersectsSet:anotherSet]; // not the best i
我正在使用以下命令构建 C++14。 g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out 如何将 -Wunused-vari
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我试图通过使用 GCC 的 -Wunused-function 标志在我的代码库中找到未使用的函数。 如我所料,使用 gcc -Wall -Wunused-function main.cpp 编译以下
这是我的代码: #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-variable" //__attribute
我使用 Ideone.com 编译了下面的代码,弹出了以下警告。 rtctimer.c: In function 'rtctimer_next_tick': rtctimer.c:87.7: warn
我已经搜索并没有真正找到并理解这个错误。奇怪的是,我只得到 c、d、e 的错误,而不是 a 和 b 或它们所有的错误。 程序是关于双链表的。 当我编译时会发生这种情况: gcc -Wall -g -c
我正在尝试使用Ubuntu将Cross编译为Android内核。 成功设置menuconfig后,并使用以下选项进行编译: make ARCH=arm CROSS_COMPILE="arm-bravo
所以我试图找出 while 循环和大多数 if 语句,所以我尝试制作这个小游戏,其中你有一定的生命值,怪物也有,只要你们都有超过 0 的生命值,循环就会运行,所以每个循环怪物都会对你造成 50 点伤害
main.cpp #include #include #include "steganography.h" // call steganography class const int arrSi
我有一个使用 SDL 的 C++ 项目。 我在编译时遇到这个错误,我不知道如何解决。 它说: Source/Classes/game.cpp:15:15: advarsel(this means 'w
我已经盯着它看了很久,但不明白为什么它会在 for 循环语句中给我一个警告。 //looks for a certain account by name in the provided list, r
我是一名优秀的程序员,十分优秀!