警告:带有 if 语句的语句 [-Wunused-value] 无效

warning: statement with no effect [-Wunused-value] with if statements

所以我试图找出 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的地址传递给scanfscanf 需要知道 action 的地址,以便它可以对其进行更改。如果您使用数组是因为 scanf 似乎不适用于简单对象,那么请不要这样做。数组可以解决您在使用 scanf 时遇到的问题,因为它们在 C 中具有某些行为,但这不是正确的解决方案。

关于:

while(health>0&&monsterhealth>0){

不要把你的代码挤在一起。写得容易阅读,间距传达意思:

while (health > 0 && monsterhealth > 0) {

关于:

(monsterhealth==monsterhealth-40);

这条语句没有效果,因为==只是比较了两个东西。该语句不会更改任何对象(变量)的值或具有任何其他可观察到的效果。它说“将 monsterhealthmonsterhealth-40 进行比较,然后不对比较结果执行任何操作。”你在这里想要的是赋值运算符,= 而不是 ==,它表示“在这个东西中放入一个新值”:

monsterhealth = monsterhealth-40;

更改您想要类似地分配新值的其他语句。

关于:

else (action==3);{(health==health+100);};

多个if-else语句的形式为:

if (condition)
    statement;
else if (condition)
    statement;
else
    statement;

请注意,它应该以 else statement 结尾,而不是 else (condition) statement。使用上面的行结构和缩进。 (有时可以将带有单个 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;

有些人提倡把所有附在ifelse语句上的语句放在大括号中,以避免某些编辑错误。对于单条语句,C语言不需要。

C 有另一种形式的语句,旨在处理您在那些 if 语句中测试的特定条件,即 switch 语句,但我认为您只是习惯了这些语句并且将会稍后了解switch

我建议您不要使用局促的 C 风格。它导致您编写了不正确的 C 并引入了错误。稍微打开它并使用传统的缩进样式。学习使用一些 C 习语,这样你就不会在应该使用 = 时使用 ==,引入 ; 等错误。你不应该去的地方等等

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(void)
{
    int monsterhealth = 100;
    int health = 100;
    int damage = 50;

    while (health > 0 && monsterhealth > 0) {
        int action;
        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 -= 40);
        }
        else if (action == 2) {
            (damage -= 30);
        }
        else if (action == 3)
        {
            (health += 100);
        }
        health = health - damage;

    }
}