c - 我无法执行在 else if 语句中包含用户输入的整数的程序

标签 c if-statement scanf

当用户输入 1,2 或 3 时,程序退出而不扫描 else if 语句 block 内的数量。

如何扫描 if 或 else if 语句内部?

if (num==4)
{
    printf("exiting");
    goto end;
}

else
{
    if (num=='1')
    {
        printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*kmstomiles;
        printf("the value of the second unit is %f",second);
    }  

     else if (num=='2')
    {
         printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*inchestofeet;
        printf("the value of the second unit is %f",second);

    }
    else if (num=='3')
    {
        printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*cmtoinches;
        printf("the value of the second unit is %f",second);
    }

最佳答案

请使用下面的代码,

if (num==4)
{
    printf("exiting");
    goto end;
}

else
{
    if (num==1)
    {
        printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*kmstomiles;
        printf("the value of the second unit is %f",second);
    }  

     else if (num==2)
    {
         printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*inchestofeet;
        printf("the value of the second unit is %f",second);

    }
    else if (num==3)
    {
        printf("enter the value of the first unit\n");
        scanf("%f",&first);
        second=first*cmtoinches;
        printf("the value of the second unit is %f",second);
    }

关于c - 我无法执行在 else if 语句中包含用户输入的整数的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56721137/

相关文章:

c - 如何将SCANF定义为以后的安全函数scanf_s()?

c - 将 unsigned char 移动超过 8 位

c - sprintf 写入字符串\n

java - 有人能告诉我为什么这个方法会进入无限循环吗?

if 语句的 Python 问题

C程序不会按升序排列数字

c - 程序在 scanf 处停止

c - 为什么 OSX 记录 atoi/atof 不是线程安全的?

c - 不匹配 c 中的工作正则表达式

scala - 在 Scala/Spark 中合并多个连续条目