如果声明未遵守其条件 [英] If statement is not following its conditional

查看:92
本文介绍了如果声明未遵守其条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code滚你只写R并按下回车键,但它似乎不读这一点,去其他的重新启动while循环。得到它推出的唯一方法是通过键入的东西大于r其他比它是一个(standard_in)1:解析错误​​

 #!/斌/庆典
#这个是一个游戏,是两个玩家的,这是一个比赛去
#100其他播放器之前回声玩家1叫什么名字?
阅读P1
回声玩家2的名字?
阅读P2
回声好吧$ p1​​和p2的$,$ P1会先走
P1S = 0
P2S = 0
PT = 1
而[$ PT -eq 1];做
回声滚动或留
选择读
如果[$选择== R];然后死亡= $(($ RANDOM%6 + 1))ELIF [$死-eq 1];然后
P1S = $(回声$ $ p1s-伯爵| BC)
回声你滚了1你的分数是$ P1S
现在$ P2转。回声
睡眠1
数= 0
PT = 2ELIF [$死-gt 1];然后
P1S = $(回声$ P1S + $死| BC)
数= $(回声$计数+ $死| BC)
回声你滚了$死去。你的分数是$ P1S
PT = 1其他如果[$选择== S];然后
回声好吧$ P1你的分数为$ P1S
现在$ P2转回声
睡眠1
数= 0
PT = 2其他
如果[$ P1S -gt 99];然后
回声$ P1亿韩元。$ P2丢失
回声你想再次玩吗?
再读
ELIF [$再次YES];然后
回声好了一秒钟。
睡眠1
明确
庆典num.sh
ELIF [$再次==无]然后
出口其他PT = 1
科幻
科幻
科幻DONE

不要担心自己不会是第二,而第二次的球员。我将那,因为它完全相同code只是球员两项。

样品输入/输出

 玩家1名?
guy1
玩家2名?
guy2
好吧guy1和guy2。 guy1会先走
滚动或留
- [R
滚动或留
RR
(standard_in)1:解析错误
你滚到5.你的分数是5
滚动或留

(standard_in)1:解析错误
你滚到5.你的分数是10
滚动或留
小号
(standard_in)1:解析错误
你滚到5.你的分数是15
滚动或留


解决方案

看适当的缩进code后(见 ghoti 的改写code段),我看到了顶层,如果块是

 如果[$选择== R];然后
    死= ... #roll模
ELIF [$死-eq 1] ...
ELIF [$死-gt 1] ...
其他
    做一点事...
科幻

问题是,如果 [$选择== R] 是真实的,你将推出芯片和跳过其余 ELIF - 其他项。所以,你会去下一个迭代没有做任何事情(除了滚动模)

解决这个问题的一种方法是检查 $选择 $死作为单独的如果块,即

 如果[$选择== R];然后
    #roll模
其他
    #break或东西...
科幻如果[$死-eq 1];然后
    #做一点事
ELIF的$死于相关检查,其余

In my code to roll you just write r and then hit enter but it seems not to read that and go to else that restarts the while loop. The only way to get it to roll is by typing something else than r than it is a (standard_in) 1: parse error.

#!/bin/bash
#this is a game that is two player and it is a race to get to 
#100 before the other player

echo "Player 1 name?"
read p1
echo "Player 2 name?"
read p2
echo "Okay $p1 and $p2. $p1 will go first"
p1s=0
p2s=0
pt=1
while [ $pt -eq 1 ]; do
echo "roll or stay"
read choice
if [ $choice == r ]; then

die=$(($RANDOM%6+1))

elif [ $die -eq 1 ]; then
p1s=$(echo "$p1s-$count" |bc)
echo "You rolled a 1. Your score is $p1s"
echo "$p2 turn now."
sleep 1
count=0
pt=2

elif [ $die -gt 1 ]; then
p1s=$(echo "$p1s+$die" |bc)
count=$(echo "$count+$die" |bc)
echo "You rolled a $die. Your score is $p1s"
pt=1

else

if [ $choice == s ]; then
echo "Okay $p1 your score is $p1s"
echo "$p2 turn now"
sleep 1
count=0
pt=2

else
if [ $p1s -gt 99 ]; then
echo "$p1 won. $p2 lost"
echo "would you like to play again?"
read again
elif [ $again  yes ]; then
echo "Okay one second."
sleep 1
clear
bash num.sh
elif [ $again == no ]; then
exit

else

pt=1
fi
fi
fi

done

Don't worry about their not being a second while for the second player. I cut that out because it same exact code just for player two.

Sample input/output

Player 1 name?
guy1
Player 2 name?
guy2
Okay guy1 and guy2. guy1 will go first
roll or stay
r
roll or stay
rr
(standard_in) 1: parse error
You rolled a 5. Your score is 5
roll or stay
roll
(standard_in) 1: parse error
You rolled a 5. Your score is 10
roll or stay
s
(standard_in) 1: parse error
You rolled a 5. Your score is 15
roll or stay

解决方案

After looking at the properly indented code (see ghoti's rewritten code segment), I see the top level if-block is

if [ $choice == r ]; then
    die=... #roll the die
elif [ $die -eq 1 ]...
elif [ $die -gt 1 ]...
else
    do something...
fi

Problem is, if [ $choice == r ] is true, you will roll the die and skip the rest of elif-else entries. So you will go to the next iteration without doing anything (except for rolling the die)

One way to fix this is to check $choice and $die as separate if blocks, namely

if [ $choice == r ]; then
    #roll the die
else
    #break or something...
fi

if [ $die -eq 1 ]; then
    #do something
elif the rest of $die-related checks  

这篇关于如果声明未遵守其条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆