【发布时间】: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>o)语法错误。正确的语法是if (something) { blablabla; } else { blobloblo; }else之后没有条件,紧跟[the block of]代码。
标签: c++ c if-statement