【问题标题】:Simple 'else if' statement is not working [duplicate]简单的“else if”语句不起作用[重复]
【发布时间】:2016-02-06 05:25:11
【问题描述】:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;

    printf("Hello world! Please enter your age\n");
    scanf("%d", &age);
    if (age <= 50) {
        printf("You are still young to change the world\n");
    }
    else if (70 >= age >50) {
        printf("You are now old, but don't worry\n");
    }
    else {
        printf("You are extremely old\n");
    }
    return 0;
}

我进入了 51 岁,它给出了“你已经非常老了”。 else if 语句不起作用。

【问题讨论】:

  • C 不支持这样的运算符链接。有关详细信息,请参阅this
  • else 属于条件(age &lt;= 50),并且在评估条件时已经强制执行(age &gt; 50)(或!(age &lt;= 50))。首先,您不需要复合条件。

标签: c


【解决方案1】:
else if ( 70>=age>50 ){

在 C 中,这不是你这样做的方式。试试这个 -

else if (age>50 && age <=70){    // age in between 50 and 70(including)

【讨论】:

  • thnxs,它的工作:))
  • age &lt;= 50 已经过测试,所以实际上if ( age &lt;= 70 ) 是最好的解决方案
猜你喜欢
  • 1970-01-01
  • 2020-04-29
  • 2022-11-15
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2015-05-05
相关资源
最近更新 更多