编程题: 编写程序实现功能:输入两个数,第一个数为被除数,第二个数为除数,如果除数为 0,则抛出一个异常并要求重新输入两个数;如果除数不为 0,则计算并显示结果。
时间: 2023-05-28 07:05:49 浏览: 77
```python
while True:
try:
dividend = int(input("请输入被除数:"))
divisor = int(input("请输入除数:"))
if divisor == 0:
raise ZeroDivisionError
result = dividend / divisor
print("计算结果为:", result)
break
except ZeroDivisionError:
print("除数不能为0,请重新输入。")
except ValueError:
print("输入的不是整数,请重新输入。")
```
相关问题
c++编程题: 编写程序实现功能:输入两个数,第一个数为被除数,第二个数为除数,如果除数为 0,则抛出一个异常并要求重新输入两个数;如果除数不为 0,则计算并显示结果。
```c
#include <stdio.h>
int main() {
int dividend, divisor, result;
while (1) {
printf("Enter dividend and divisor: ");
scanf("%d%d", ÷nd, &divisor);
if (divisor == 0) { // 如果除数为 0,抛出异常并重新输入
printf("Divisor can not be zero! Please enter again.\n");
continue;
}
result = dividend / divisor; // 计算结果
printf("%d / %d = %d\n", dividend, divisor, result);
break;
}
return 0;
}
```
C语言编程实现:输入两个整数,将这两个数按从小到大的顺序输入
下面是C语言实现的代码:
```c
#include <stdio.h>
int main() {
int a, b, temp;
printf("请输入两个整数:");
scanf("%d %d", &a, &b);
if (a > b) {
temp = a;
a = b;
b = temp;
}
printf("从小到大的顺序是:%d %d", a, b);
return 0;
}
```
首先通过 `scanf` 函数获取用户输入的两个整数,然后通过 `if` 语句判断大小关系,如果第一个数大于第二个数,就交换它们的值,最后通过 `printf` 函数输出结果。
相关推荐
![sb3](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)