【问题标题】:warning: statement with no effect [-Wunused-value] with if statements警告:没有效果的语句 [-Wunused-value] 与 if 语句
【发布时间】:2019-08-09 11:37:14
【问题描述】:

所以我试图弄清楚 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;

    }
}

【问题讨论】:

  • damage==damage-30 --> damage=damage-30 即只有一个=
  • health==health+100 ...再次...只是一个=
  • monsterhealth==monsterhealth-40 ...再次
  • else (action==3); 嗯...; 好像很奇怪...我猜你想删除它
  • 哦,我看错了

标签: c if-statement compiler-errors comparison-operators date-comparison


【解决方案1】:

关于:

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 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;

有些人主张将所有附加到if 和else 语句的语句放在大括号中,以避免某些编辑错误。对于单个语句,C 语言不需要它。

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

【讨论】:

    【解决方案2】:

    我建议您不要使用狭窄的 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;
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 2017-07-16
      相关资源
      最近更新 更多