как перезапустить оператор if - PullRequest
0 голосов
/ 13 апреля 2020

Извините, если название не указано c, но мне нужна помощь: я пытаюсь сделать небольшую игру в догадки, и я пытаюсь реализовать подсказку

  #!/bin/bash
echo "what comes once in a minute, twice in a moment, but never in a thousand years?
(lowercase)"
read input
if [ $input = "m" ] ; then
clear
echo "correct"
sleep 4.4
clear
elif [ $input = "hint" ] ; then
clear
echo "its a letter"   #what do i do so that after it prints "its a letter"
clear                 #it comes back to the beginning of the if statement 
else
clear
echo "incorrect"
sleep 4.4
clear
fi

, например: я бегу скрипт

-печатает what comes once in a minute, twice in a moment, but never in a thousand years? -i ответ hint и после подсказки возвращается к ожиданию ввода ответа

Ответы [ 2 ]

1 голос
/ 13 апреля 2020

Сделать это функцией с рекурсией

game () {
    read -p "what comes once in a minute, twice in a moment, but never in a thousand years?
    (lowercase) " input # make read print it

    # use case instead of if\then
    case $input in
     "hint") clear; echo "its a letter"; game;;
        "m") clear; echo "correct"; sleep 4.4; game;;
          *) clear; echo "incorrect"; sleep 4.4; game;;
    esac 
}

game
0 голосов
/ 13 апреля 2020

Возможно, вы хотите получить больше вопросов / ответов и хотите сделать что-то вроде

quiz() {
   if [[ $# -ne 3 ]]; then
      echo 'Call this function like quiz "question" "hint" "answer"'
      echo "All arguments in single or double quotes."
   fi
   # Now implement something with `while` and use `break` after a good answer of give-up
   # ... your code ;-)
}

# Advanced: Store questions/hints/answers in an array and use a while loop 

set1_q='what comes once in a minute, twice in a moment, but never in a thousand years?'
set1_h='its a letter'
set1_a='m'
# set1_e for an explanation?
set2_q='Solve x in (x-a).(x-b).(x-c). ... (x-z) = x'
set2_h='Did you notice (x-x)?'
set2_a='0'

quiz "${set1_q}" "${set1_h}" "${set1_a}"
quiz "${set2_q}" "${set2_h}" "${set2_a}"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...