c语言有一个函数 y={ x, -5<x<0 x -1,x=0 x+1,0<x<10} 分别用简单的语句,嵌套的if语句,if-else语句,要求输入x的值,输出y的值
时间: 2024-05-16 10:12:16 浏览: 11
简单的语句:
```
#include <stdio.h>
int main() {
int x, y;
scanf("%d", &x);
if (x > 0 && x < 10) {
y = 1;
} else if (x == 0) {
y = -1;
} else if (x > -5 && x < 0) {
y = x;
} else {
y = x - 1;
}
printf("%d\n", y);
return 0;
}
```
嵌套的if语句:
```
#include <stdio.h>
int main() {
int x, y;
scanf("%d", &x);
if (x > -5 && x < 0) {
if (x == -1) {
y = -1;
} else {
y = x;
}
} else if (x == 0) {
y = -1;
} else if (x > 0 && x < 10) {
y = 1;
} else {
y = x - 1;
}
printf("%d\n", y);
return 0;
}
```
if-else语句:
```
#include <stdio.h>
int main() {
int x, y;
scanf("%d", &x);
if (x > 0 && x < 10) {
y = 1;
} else {
if (x == 0) {
y = -1;
} else {
if (x > -5 && x < 0) {
y = x;
} else {
y = x - 1;
}
}
}
printf("%d\n", y);
return 0;
}
```
笑话:
为什么狮子不会玩扑克牌?因为他们都是狮子座。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)