① 编写程序显示某一科考试成绩,从键盘输入数据,当输入100时显示“满分”、90-99显示“优秀”、80-89显示“良好”、70-79显示“中等”、60-69显示“及格”、 0-59显示“不及格”;其他输入则显示“非法输入”。请用 “try-catch”改写。
时间: 2023-10-02 15:07:06 浏览: 29
下面是使用 try-catch 语句来改写的代码:
```python
try:
score = int(input("请输入考试成绩:"))
if score == 100:
print("满分")
elif score >= 90 and score <= 99:
print("优秀")
elif score >= 80 and score <= 89:
print("良好")
elif score >= 70 and score <= 79:
print("中等")
elif score >= 60 and score <= 69:
print("及格")
elif score >= 0 and score <= 59:
print("不及格")
else:
print("非法输入")
except ValueError:
print("非法输入")
```
如果用户输入的不是整数,会触发 ValueError 异常,此时程序会输出“非法输入”。如果用户输入的是整数,程序会根据输入的数值输出相应的考试评价。
相关问题
java 编写程序显示某一科考试成绩,从键盘输入数据,当输入100时显示“满分”、90-99显 示“优秀”、80-89显示“良好”、70-79显示“中等”、60-69显示“及格”、0-59显示“不及 格”;其他输入则显示“非法输入”。
```java
import java.util.Scanner;
public class ExamScore {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入考试成绩:");
int score = scanner.nextInt();
if (score == 100) {
System.out.println("满分");
} else if (score >= 90 && score <= 99) {
System.out.println("优秀");
} else if (score >= 80 && score <= 89) {
System.out.println("良好");
} else if (score >= 70 && score <= 79) {
System.out.println("中等");
} else if (score >= 60 && score <= 69) {
System.out.println("及格");
} else if (score >= 0 && score <= 59) {
System.out.println("不及格");
} else {
System.out.println("非法输入");
}
}
}
```
用JAVA 编写程序显示某一科考试成绩,从键盘输入数据,当输入100时显示“满分”、90-99显示“优秀”、80-89显示“良好”、70-79显示“中等”、60-69显示“及格”、 0-59显示“不及格”;其他输入则显示“非法输入”。请用 “try-catch”改写。
以下是使用 try-catch 语句来改写的代码:
```java
import java.util.Scanner;
public class ExamScore {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
int score = sc.nextInt();
if (score == 100) {
System.out.println("满分");
} else if (score >= 90 && score <= 99) {
System.out.println("优秀");
} else if (score >= 80 && score <= 89) {
System.out.println("良好");
} else if (score >= 70 && score <= 79) {
System.out.println("中等");
} else if (score >= 60 && score <= 69) {
System.out.println("及格");
} else if (score >= 0 && score <= 59) {
System.out.println("不及格");
} else {
System.out.println("非法输入");
}
} catch (Exception e) {
System.out.println("非法输入");
}
sc.close();
}
}
```
使用 try-catch 语句可以捕获用户输入的异常,避免程序崩溃。如果用户输入非数字字符或者超出整数范围,程序会抛出异常,此时我们可以在 catch 块中处理异常情况,输出提示信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)