编写⼀段shell程序实现以下功能: 根据键盘输⼊的学⽣成绩,显示相应的成绩等级, 其中 60分以下为"Failed!", 60~69分为"Passed!", 70~79分为"Medium!", 80~89分为"Good!", 90~100为"Excellent!"。 如果输⼊超过100的分数,则显示"error score"; 如果输⼊负数,则退出程序,否则⼀直提示⽤户输⼊成绩。
时间: 2023-07-19 12:11:22 浏览: 43
可以使用如下 Shell 脚本实现:
```shell
#!/bin/bash
while true; do
read -p "请输入成绩: " score
if [[ $score -lt 0 ]]; then
echo "输入成绩为负数,退出程序"
exit 0
elif [[ $score -lt 60 ]]; then
echo "Failed!"
elif [[ $score -lt 70 ]]; then
echo "Passed!"
elif [[ $score -lt 80 ]]; then
echo "Medium!"
elif [[ $score -lt 90 ]]; then
echo "Good!"
elif [[ $score -le 100 ]]; then
echo "Excellent!"
else
echo "error score"
fi
done
```
说明:
1. 使用 while 循环不断提示用户输入成绩,直到用户输入负数或者其他非法字符退出程序;
2. 使用 read 命令读取用户输入的成绩,并将其保存到变量 score 中;
3. 使用 if-elif-else 语句对成绩进行判断,并输出相应的成绩等级;
4. 当用户输入负数时,输出提示信息并退出程序;
5. 当用户输入超过 100 的分数时,输出提示信息。
相关推荐
![sh](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![sh](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![sh](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)