If statement is not following its conditional

前端 未结 2 1028
谎友^
谎友^ 2021-01-27 14:40

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 som

2条回答
  •  旧时难觅i
    2021-01-27 15:37

    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  
    

提交回复
热议问题