|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#in#include<stdio.h>
#include<math.h>
int main()
{
int x,y;
scanf("x=%d",&x);
if(x<2)
y=x;
else
if(x<=6)
y=x*x+1;
else
if(x<10)
y=sqrt(x+1);
else
y=(1/(x+1));
printf("y=%d",y);
return 0;
}
本帖最后由 sunrise085 于 2020-3-25 11:08 编辑
if或else的语句块只有一句的话,可以不加大括号。
若加的话,就是将整个语句块都放进大括号内
你的程序中将xy定义成int,在计算过程中会出错的。
另外scanf用法有点问题,引号内建议只写格式化字符。
第二个if的条件写错了。
- //不加大括号
- #include<stdio.h>
- #include<math.h>
- int main()
- {
- float x=6,y;
- scanf("%f",&x);
- if(x<2)
- y=x;
- else
- if(x<6)
- y=x*x+1;
- else
- if(x<10)
- y=sqrt(x+1);
- else
- y=(1/(x+1));
- printf("y=%f",y);
- return 0;
- }
复制代码
- //加大括号
- #include<stdio.h>
- #include<math.h>
- int main()
- {
- float x=6,y;
- scanf("%f",&x);
- if(x<2)
- {
- y=x;
- }
- else
- {
- if(x<6)
- {
- y=x*x+1;
- }
- else
- {
- if(x<10)
- {
- y=sqrt(x+1);
- }
- else
- {
- y=(1/(x+1));
- }
- }
- }
- printf("y=%f",y);
- return 0;
- }
复制代码
|
|