【问题标题】:if else statement not giving expected answerif else 语句没有给出预期的答案
【发布时间】:2015-10-10 04:40:09
【问题描述】:

输入的值与输出等级不匹配,因为即使不满足 10 的条件,它也会给出 10 等级。 问题是,在进入硬度 50 强度 5600 和碳 0.7 时,其给出等级 10 而对于等级 10,碳应该小于 0.7? #包括 #包括 #包括

int main() {
    // program grade the steel on quality basis

    int hardness;
    int strength;
    float carbon;

    printf("Enter the hardness of steel:");    // condition 1  hardness should be >= 50
    scanf("%d", &hardness);

    printf("Enter the tensile strength:");     // condition 2  strength should be >= 5600
    scanf("%d", &strength);

    printf("Enter carbon content:");           // condition 3  carbon less than 0.7
    scanf("%.2f", &carbon);


    if ((hardness >= 50) && (carbon < 0.7) && (strength >= 5600)) {        // all true
        printf("\ngrade = 10");
    }
    else if ((hardness >= 50) && (carbon < 0.7) && (strength <= 5600)) {     // 1 and 2 true
        printf("\ngrade = 9");
    }
    else if ((hardness <= 50) && (carbon < 0.7) && (strength >= 5600)) {    // 2 and 3 true
        printf("\ngrade = 8");
    }
    else if ((hardness >= 50) && (carbon > 0.7) && (strength >= 5600)) {    // 1 and 3 true
        printf("\ngrade = 7");
    }
    else if ((hardness >= 50) && (carbon > 0.7) && (strength <= 5600)) {    // any one true
        printf("\ngrade = 6");
    }
    else if ((hardness <= 50) && (carbon < 0.7) && (strength <= 5600)) {    // any one true
        printf("\ngrade = 6");
    }   
    else if ((hardness <= 50) && (carbon < 0.7) && (strength >= 5600)) {    // any one true
        printf("\ngrade = 6");
    }
    else {
        printf("\ngrade = 5");                     // none true
    }

    _getch();
    return 0;
}

【问题讨论】:

  • case 适合这个...
  • scanf("%.2f", &amp;carbon);更改为scanf("%f", &amp;carbon);
  • 您可能会注意到,如果碳含量正好是 0.7(这是否真的是一个微妙的问题 - 可能不是因为 carbonfloat0.7 是 @ 987654328@),那么你永远不会匹配这个值。

标签: c if-statement case


【解决方案1】:

scanf 中使用"%.2f" 作为格式说明符是不正确的。这对printf 有好处,但对scanf 没有好处。

最好始终检查scanf 的返回值,以确保函数能够读取预期的数据。

if ( scanf("%.2f", &carbon) != 1 )
{
   // Deal with error.
}

向其他 scanf 调用添加类似的检查。

我认为将上述格式说明符更改为"%f" 应该可以解决您的问题。仍然添加检查。

if ( scanf("%f", &carbon) != 1 )
{
   // Deal with error.
}

【讨论】:

  • 并添加对其他两个 scanf() 调用的检查。打印出输入以确保程序获得您认为的值总是一个好主意。
【解决方案2】:

问题在于 scanf 语句中的格式说明符(.2f)。 %.2f 一般用于在点后打印 2 位数字 在 C 中,所有浮点字面量都存储为双精度值。因此我们需要通过 f 附加值来指定我们使用的是浮点数,即单精度。检查代码的更改 如果您希望您的碳四舍五入到 2 位精度,您可以使用 carbon = ceilf(carbon*100)/100.0; 当您下次发布您的问题时,发布您失败的输入,您将得到快速回复 你可以用更少的比较来写这个 - 这是工作代码

#include<stdio.h>
int main(void) {
int hardness;
int strength;
float carbon;
printf("Enter the hardness of steel:"); 
scanf("%d", &hardness);
printf("Enter the tensile strength:"); 
scanf("%d", &strength);
printf("Enter carbon content:");
scanf("%f", &carbon);
if ((hardness >= 50) && (carbon < 0.7f) && (strength >= 5600))
    printf("\ngrade = 10");
else if ((hardness >= 50) && (carbon < 0.7f))
    printf("\ngrade = 9");
else if ((carbon < 0.7f) && (strength >= 5600))   
    printf("\ngrade = 8");
else if ((hardness >= 50) && (strength >= 5600)) 
    printf("\ngrade = 7");
else if ((hardness >= 50) || (carbon > 0.7f) || (strength <= 5600))
    printf("\ngrade = 6");
else
    printf("\ngrade = 5");                  
_getch();
return 0;
}

【讨论】:

  • 仍然存在的问题是,在输入硬度 50 强度 5600 和碳 0.7 时,其给出等级 10 而对于等级 10,碳应小于 0.7
  • [叹气] 0.69999999999999999999999999999999999 小于 0.7
猜你喜欢
  • 2020-05-24
  • 2014-12-13
  • 2021-09-23
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多