代码中的逻辑错误,只允许在 bash 脚本中提出一个问题(我认为)

问题描述 投票:0回答:0

我正在尝试创建一个输出错误单词的代码,用户必须输入单词的正确拼写。但是,目前它只输出一个问题,总共大约有 10 个。此时它正确输出第一题,输入正确的拼写被告知正确,然后测验结束。

代码如下:

for (( i=0; i<$question; i++ )); do
  current_pair=${shuffled_word_pairs[$i]}
  misspelled_word=$(echo "$current_pair" | cut -d',' -f1)
  correct_word=$(echo "$current_pair" | cut -d',' -f2)

  if [[ "$misspelled_word" == "" ]]; then
    continue
  fi

  echo "Question $questions: What is the correct spelling of of '$misspelled_word'?"

  read -p "Your answer: " answer

  while [ "$answer" != "$correct_word" ]; do
    if [[ "$answer" == "skip" ]]; then
      echo "Question skipped."
      break
    elif [[ "$answer" == "help"* ]]; then
      echo "Hint: The first letter of the correct word is '$(echo "$correct_word" | cut -         c1)'."
      read -p "Your answer: " answer
    elif [[ "$answer" == "many"* ]]; then
      echo "Incorrect. The correct answer is '$correct_word'."
      read -p "Your answer: " answer
    elif [ "$allow_multiple_attempts" = true ]; then
      read -p "Incorrect. Please try again: " answer
    else
      echo "Incorrect. The correct answer is '$correct_word'."
      break
    fi
  done

  if [ "$answer" = "$correct_word" ]; then
    echo "Correct!"
    score=$((score++))
  fi

  questions=$((questions+1))
 done

我查看了两个 shellscript 并尝试重新组织代码的结构,但似乎没有一个对我有用。 Shellscript 表示没有重大错误,而只是代码上的信息点。

任何帮助将不胜感激, 谢谢

bash ubuntu logic
© www.soinside.com 2019 - 2024. All rights reserved.