编程:统计输入的若干个数中负数、零及正数的个数(输入^z控制循环结束)
时间: 2024-05-09 14:19:17 浏览: 13
```python
positive_count = 0
negative_count = 0
zero_count = 0
while True:
try:
num = float(input())
if num > 0:
positive_count += 1
elif num < 0:
negative_count += 1
else:
zero_count += 1
except:
break
print("Positive numbers:", positive_count)
print("Negative numbers:", negative_count)
print("Zeros:", zero_count)
```
注意:这里使用`try-except`语句来捕获输入结束的异常,因为在输入结束后`input()`函数会抛出一个`EOFError`异常。
相关问题
用c语言编程:统计输入的若干个数中负数、零及正数的个数(输入^z控制循环结束)
```c
#include <stdio.h>
int main()
{
int num, positive = 0, negative = 0, zero = 0;
while (scanf("%d", &num) != EOF)
{
if (num > 0)
positive++;
else if (num < 0)
negative++;
else
zero++;
}
printf("输入的负数个数为%d,零的个数为%d,正数的个数为%d\n", negative, zero, positive);
return 0;
}
```
用c语言编编程:统计输入的若干个数中负数、零及正数的个数(输入^z控制循环结束)
```c
#include <stdio.h>
int main() {
int num, neg = 0, zero = 0, pos = 0;
while (scanf("%d", &num) != EOF) {
if (num < 0) {
neg++;
} else if (num == 0) {
zero++;
} else {
pos++;
}
}
printf("Negative: %d\n", neg);
printf("Zero: %d\n", zero);
printf("Positive: %d\n", pos);
return 0;
}
```
相关推荐
![](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)