【问题标题】:why is my program running both the if and else conditions?为什么我的程序同时运行 if 和 else 条件?
【发布时间】:2021-12-18 12:27:51
【问题描述】:

我是编码新手。自从我开始学习 c 语言以来已经有 2 到 3 周了。我从 Youtube 教程中学会了使用 if 和 else 语句,并制作了这个小代码块:

#include <stdio.h>
#include <stdlib.h>
  float c,f;
main()
{
     int t=2;
     int o;
     printf("select any one option by entering '1' or '3'  \n 1. celcius to farenheit  \n 3. farenheit to celcius  \n enter any option ");
     scanf("%d",&o);
        if(t<o)
         { 
          printf(" \n enter the value of temperature in celcius ");
          scanf("%f",&c);
          f=(c*1.8)+32;
          printf(" \n the value of temperature of %f celcius in farenheit is %f ",c,f);
         }
        else(t>o);
         {
          printf("  \n enter the value of temperature in farenheit ");
          scanf("%f",&f);
          c=(f-32)/1.8;
          printf("  \n the value of temperature of %f farenheit in celcius is %f ",f,c);
         }
         return 0;
}

我也试过这种方式

#include <stdio.h>
#include <stdlib.h>
 float c,f;
 
main()
{
     int o;
     printf("select any one option by entering 'a' or 'b'  \n 1. celcius to farenheit  \n 2. farenheit to celcius  \n enter any option ");
     scanf("%d",&o);
        if(o=1)
         { 
          printf(" \n enter the value of temperature in celcius ");
          scanf("%f",&c);
          f=(c*1.8)+32;
          printf(" \n the value of temperature of %f celcius in farenheit is %f ",c,f);
         }
        else(o=2);
         {
          printf("  \n enter the value of temperature in farenheit ");
          scanf("%f",&f);
          c=(f-32)/1.8;
          printf("  \n the value of temperature of %f farenheit in celcius is %f ",f,c);
         }
}

所以问题是这段代码一个接一个地运行这两个条件。所以请批评一下,如果你有的话,也向我推荐一些好的初学者 C 语言自学书籍或教程。谢谢。

【问题讨论】:

  • if(o=1) ==> if (o == 1) ...分配与比较
  • else 中的 (t&gt;o) 语法错误。正确的语法是if (something) { blablabla; } else { blobloblo; }else之后没有条件,紧跟[the block of]代码。

标签: c++ c if-statement


【解决方案1】:

首先:

else(o=2); // <-- notice the semicolon
{
      printf("  \n enter the value of temperature in farenheit ");
      scanf("%f",&f);
      c=(f-32)/1.8;
      printf("  \n the value of temperature of %f farenheit in celcius is %f ",f,c);
}

将被提升到:

else
{
    o = 2;
}
// this will run no matter the condition
{
      printf("  \n enter the value of temperature in farenheit ");
      scanf("%f",&f);
      c=(f-32)/1.8;
      printf("  \n the value of temperature of %f farenheit in celcius is %f ",f,c);
}

其次,if(o=1)o 分配给1,因为o 是一个非零值,if(o=1) 每次都会返回true。 你应该改用==

第三,你else有错误的systax,else systax看起来像这样

if (somecondition)
{
   // code
}
else
{
   // code
}

根据你写的内容,你应该改用else if

您的代码应如下所示:

if(o == 1)
{
    // code
}
else if(o == 2)
{
    // code
}

【讨论】:

    【解决方案2】:

    如果要检查相等条件,需要将 = 替换为 ==。 此外,您还可以使用 else-if 语句,因此您的代码将是:

    if(t<o){
        //actions
    }
    else if(t>o){
        //actions
    }
    

    【讨论】:

    • “所以你的代码将是:” - 什么都没有?
    • @Llama 我更正了答案
    【解决方案3】:

    你有一个;,否则你需要删除它。

    else//;REMOVED THIS SEMICOLON and no need to pass arguments or use else if
    

    请注意,在 else 语法中的第一个代码 sn-p 中,您不需要使用 () 。其次,您在 else 之后有一个分号 ;,它不需要存在。

    在您的第二个代码 sn-p 中,您使用的是 =,而您应该使用的是 ==

    您可以将else if 用于您的第一个代码 sn-p,如下所示:

    else if(t > o)
    {
       ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 2017-12-17
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      相关资源
      最近更新 更多