【问题标题】:How do I solve the error: switch quantity not an integer and error: case label does not reduce to a an integral constant"如何解决错误:开关量不是整数和错误:案例标签不会减少为整数常量”
【发布时间】:2020-11-12 12:14:33
【问题描述】:

我正在使用 C 语言编写程序,该程序根据他们在 MVP 等各种标准中获得的平均分来选择项目团队中的员工,所以我得到的错误出现在 switch 声明中,上面写着 error: switch quantity not an integer,而第二个错误是error: case label does not reduce to a an integral constant

代码如下:

#include <stdio.h>

main() {
    printf("Google Team Selection\n\n");
    double a, b, c, sum, average;
    printf("Enter your Minimum Viable Product(MVP) points here:\n");
    scanf("%lf", &a);
    printf("Enter your Apprenticer Programmer points here:\n");
    scanf("%lf", &b);
    printf("Enter your consistency points here:\n");
    scanf("%lf", &c);
    printf("Enter your qocient points");
    sum = a + b + c;
    average = sum / 3.0;
    printf(" the sum is %lf and the average is %lf", sum, average);
    switch (average) {
      case ">55":
        printf("Congratulations you have been selected\n");
        break;
    }
}

所以,我希望我的程序选择平均水平高于55 的员工。

【问题讨论】:

  • 抱歉,这与 StrongLoop 有什么关系?根据标签描述:“StrongLoop 是一个 API 层,用于将企业数据连接到设备和浏览器。它包括 LoopBackJS、StrongOps 和 StrongNode。”
  • 由任何体面的书籍/教程回答,或在 switch 语句中询问范围/更大/更小的各种问题的副本,例如Larger than and less than in C switch statement

标签: c


【解决方案1】:

Switch 需要一个整数或一个字符或一个输出为 int 或 char 的表达式。案例也需要一个常量表达式。您可以查看switch microsoft documentation

在这种情况下,您应该只使用 if-else。

所以:

if (average > 55) {
   printf("Congratulations you have been selected\n");
}

【讨论】:

    【解决方案2】:

    您不能在 C 中使用 switch 来达到您的目的:switch 表达式 (average) 和 case 表达式必须是整数。

    您应该使用简单的if 语句:

        if (average > 55) {
            printf("Congratulations you have been selected\n");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多