我应该:
制作一个 case 语句,输入后将显示 a 等级。没有循环或条件。
- A=90-100、B=80-89、C=70-79、D=60-69、F=0-59
这是我到目前为止所拥有的:
echo -n "What is your test score? (in percentage): "
read percentage
case $name in
9?) echo "A" ;;
8?) echo "B" ;;
7?) echo "C" ;;
6?) echo "D" ;;
5?) echo "F" ;;
4?) echo "F" ;;
3?) echo "F" ;;
2?) echo "F" ;;
1?) echo "F" ;;
[0-9]) echo "F" ;;
*) echo "Please enter another number"
read percentage
;;
esac
如何用范围(例如 59-0)做出声明?
答案1
我会这样做,它应该满足要求。
#!/bin/bash
echo -n "What is your test score? "
read score
case $score in
100) echo "A" ;;
9[0-9]) echo "A" ;;
8[0-9]) echo "B" ;;
7[0-9]) echo "C" ;;
6[0-9]) echo "D" ;;
*) echo "F" ;;
esac
最初我使用 9[0-9]|100) 但我认为这违反了条件语句的规则。
如果您尝试输入 101 或任何其他“超出范围”的值,它将返回 F。我假设这永远不会发生。
答案2
这也将起作用-
echo -n "What is your test score? (in percentage): "
read per
case $per in
([9][0-9]|100) echo "A" ;;
([8][0-9]) echo "B" ;;
([7][0-9]) echo "C" ;;
([6][0-9]) echo "D" ;;
[0-9]|[1-5][0-9]|59) echo "F" ;;
*) echo "Please enter another number"
read percentage;;
esac