【问题标题】:States of Water (temperature program) - C programming水的状态(温度程序) - C 编程
【发布时间】:2016-10-11 04:58:44
【问题描述】:

我正在开发一个程序,我必须在 if/else 中使用布尔运算符从用户那里获取温度值和度数符号,并打印出水所处的状态(冰、液体、蒸汽)。

输入输出示例:

   Enter temperature, such as 31 F: 101c
   Water is steam at 101c.

我的代码发布在下面。似乎它应该可以工作,但我认为我没有正确输入,因此没有输入循环。不知道如何修复它。

#include <stdio.h>


int main(void)
{

    double temp;
    char CorF;

    printf("Please enter a temperature, such as 31 F:");

    while (scanf_s("%lf %c", &temp, &CorF, 2) == 1)
    {

        if (temp <= 32 && CorF == 'F')
        {
            printf("Water is ice at %lf %c.\n", temp, CorF);
        }
        else if (temp >= 212 && CorF == 'F')
        {
            printf("Water is steam at %lf %c.\n", temp, CorF);
        }
        else if (temp > 32 && temp < 212 && CorF == 'F')
        {
            printf("Water is liquid at %lf %c.\n", temp, CorF);
        }
        else if (temp <= 0 && CorF == 'C')
        {
            printf("Water is ice at %lf %c.\n", temp, CorF);
        }
        else if (temp >= 100 && CorF == 'C')
        {
            printf("Water is steam at %lf %c.\n", temp, CorF);
        }
        else if (temp < 0 && temp > 100 && CorF == 'C')
        {
            printf("Water is liquid at %lf %c", temp, CorF);
        }
        else
        {
            // blank line!
        }
    }

    return 0;

}

【问题讨论】:

  • scanf 返回成功转换的次数,因此 good 在您的情况下返回值为 2。此外,如果用户 确实 输入101c,因为'c''C'不一样,所以条件都不满足
  • scanf_s("%lf %c", &amp;temp, &amp;CorF, 2) == 1 --> scanf_s("%lf %c", &amp;temp, &amp;CorF, 1) == 2
  • temp &lt; 0 &amp;&amp; temp &gt; 100 --> temp &gt; 0 &amp;&amp; temp &lt; 100
  • 当您确定“我的循环没有被输入”时,与其花时间在这里发布问题,不如问问自己“为什么会这样?”...答案,如果您不确定,是实验。问“我想知道sscanf_s 正在返回什么值”。使用调试器,或更改代码以存储和显示结果。这种探索和好奇的基本技能将使您免于询问 99% 的问题,否则您可能会在本网站上提出这些问题。

标签: c if-statement while-loop temperature


【解决方案1】:

您的代码中有很多错误。 While 是一个循环,它用于循环。当您使用 scanf_s 检查它时,它会在每次进入循环时检查该值。 而不是使用 while 你应该使用 if 语句。

#include<stdio.h>
int main(void)

{


double temp;
char CorF;

printf("Please enter a temperature, such as 31 F:");
int i=scanf("%lf %c", &temp, &CorF);
if(i==2)
{

    if (temp <= 32 && CorF == 'F')
    {
        printf("Water is ice at %lf %c.\n", temp, CorF);
    }
    else if (temp >= 212 && CorF == 'F')
    {
        printf("Water is steam at %lf %c.\n", temp, CorF);
    }
    else if (temp > 32 && temp < 212 && CorF == 'F')
    {
        printf("Water is liquid at %lf %c.\n", temp, CorF);
    }
    else if (temp <= 0 && CorF == 'C')
    {
        printf("Water is ice at %lf %c.\n", temp, CorF);
    }
    else if (temp >= 100 && CorF == 'C')
    {
        printf("Water is steam at %lf %c.\n", temp, CorF);
    }
    else if (temp < 0 && temp > 100 && CorF == 'C')
    {
        printf("Water is liquid at %lf %c", temp, CorF);
    }
    else
    {
       ; // blank line!
    }
}

return 0;
}

您可以使用此代码。 from scanf:成功时,函数返回成功读取的项目数。如果发生匹配失败,此计数可以匹配预期的读数数量或更少,甚至为零。如果在成功读取任何数据之前输入失败,则返回 EOF。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多