【发布时间】:2013-11-18 09:24:45
【问题描述】:
所以我正在linux终端上编写一个程序,我的程序有两个部分。第一部分是除法,第二部分是计算一些数字的 MOD。退出第一部分的方法是将 999 放入任何一个要除的输入中。
我的问题是,即使用户输入 999 作为第一个输入,用户也必须输入第二个数字。我想知道这些是否类似于在 Windows 中可以执行 goto :someOtherLocation 在 linux 中的东西。这是代码:
echo "Enter the number to divide (dividend) (enter 999 to quit):"
read numberOne
[IF NUMBERONE = 999, JUMP TO SECONDPART]
echo "Enter the number to divide (divisor) (enter 999 to quit):"
read numberTwo
while [ "$numberOne" -ne '999' ] && [ "$numberTwo" -ne '999' ]
do
while [ "$numberTwo" -eq 0 ]
do
echo "You cannot divide by 0, please enter another number:"
read numberTwo
done
RESULT=$(echo "$numberOne/$numberTwo" | bc -l)
echo $numberOne / $numberTwo = $RESULT
echo $numberOne / $numberTwo = $RESULT >> results.txt
echo "Enter the number to divide (dividend) (enter 999 to quit):"
read numberOne
echo "Enter the number to divide (divisor) (enter 999 to quit):"
read numberTwo
done
SECONDPART
counter=1
totalCount=0
temporal=0
while [ "$counter" -lt '101' ]
do
temporal=$( expr $counter % 5)
echo $counter MOD 5 = $temporal
echo $counter MOD 5 = $temporal >> results.txt
totalCount=$(echo "$totalCount+$temporal" | bc -l)
counter=$(echo "$counter+1" | bc -l)
done
average=$(echo "$totalCount/100" | bc -l)
echo The average of all the MODs is $average >> results.txt
如上所示,如果输入为 999,我想直接从输入跳转到第二部分。
【问题讨论】:
-
正常的
if语句有什么问题? -
“Linux”和“Linux 终端”不是编程语言。我想你的意思是“Bash”。
-
@IgnacioVazquez-Abrams ,没什么,我想做一个 If,但我问如果它是一个 '999' 的情况下在 if 里面放什么,所以它跳到第二部分代码。
-
你没有。只有当它不是时,你才让它运行第一部分。
-
@IgnacioVazquez-Abrams 哦,所以
if 999 ( do the first part), else jump to second?听起来可以解决问题
标签: linux bash loops terminal sh