0

我现在只使用鞋子几天,所以也许我错过了一些东西。我给儿子写了一个小程序来帮助他学习乘法表。当他答对十个时,他就完成了。我似乎无法使用 SHOES 正确获取 while 循环。有人请给我看一个while语句。当我尝试时;它要么消除我的流程和堆栈语句,要么消除 Shoe 崩溃。

提前致谢。山姆

4

1 回答 1

5

我不知道你是如何使用while循环的。很可能您正在尝试通过while循环每次迭代重新创建堆栈,这是一个坏主意。立即想到的两个解决方案是处理按钮单击的逻辑并连续跟踪正确的数字,如下所示:

Shoes.app do
  num_correct = 0
  first_num = 1 + rand(10)
  second_num = 1 + rand(10)
  answer = first_num * second_num

  stack do
    @info = para 'Hi, Timmy!  This program will test your ',
           'multiplication tables.  When you get 10 ',
           'correct, you get to stop, and you get your ',
           'pet hamster back!'
    @question = para "What is #{first_num} x #{second_num}?"
    @response = edit_line :width => 100
    btn = button 'OK' do
      if @response.text == ''
        alert('You need to put an answer in the box, Timmy.')
      elsif @response.text.to_i == answer
        num_correct += 1
        if num_correct == 10
          @info.text = "Good job!  That's #{num_correct} in a row!"
          alert('You did it, Timmy!  You can have your ' \
                  'hamster back... for now.')
          exit
        else
          @info.text = "Good job!  That's #{num_correct} in a row!"
          first_num = 1 + rand(10)
          second_num = 1 + rand(10)
          answer = first_num * second_num
          @question.text = "What is #{first_num} x #{second_num}?"
          @response.text = ''
        end
      else
        num_correct = 0
        @info.text = "Wrong, Timmy.  The answer is #{answer}."
        first_num = 1 + rand(10)
        second_num = 1 + rand(10)
        answer = first_num * second_num
        @question.text = "What is #{first_num} x #{second_num}?"
        @response.text = ''
      end
    end
  end
end

或者,我认为更有趣的解决方案是使用urland visit

class MyTest < Shoes

  url '/', :index
  url '/correct/(\d+)', :correct
  url '/wrong/(\d+)', :wrong
  url '/question', :question
  url '/question/(\d+)', :question
  url '/done', :done

  def index
    stack do
      para 'Hi, Timmy!  This program will test your ' \
             'multiplication tables.  When you get 10 ' \
             'correct, you get to stop, and you get your ' \
             'pet hamster back!'
      button 'OK' do 
        visit '/question'
      end
    end
  end

  def question(num_correct = 0)
    num_correct = num_correct.to_i
    first_num = 1 + rand(10)
    second_num = 1 + rand(10)
    answer = first_num * second_num
    stack do
      para "What is #{first_num} x #{second_num}?"
      flow do
        response = edit_line :width => 100
        button 'Answer' do
          if response.text.to_i == answer
            num_correct += 1
            if num_correct == 10
              visit '/done'
            else
              visit "/correct/#{num_correct}"
            end
          else
            visit "/wrong/#{answer}"
          end
        end
      end
    end
  end

  def correct(num_correct)
    stack do
      para "Good job!  That's #{num_correct} in a row!"
      button "Next Question" do
        visit "/question/#{num_correct}"
      end
    end
  end

  def wrong(answer)
    @num_correct = 0
    stack do
      para "Wrong!  The correct answer is #{answer}."
      button "Next Question" do
        visit '/question'
      end
    end
  end

  def done
    stack do
      para 'You did it, Timmy!  You can have your ' \
      'hamster back... for now.'
      button 'OK' do
        exit
      end
    end
  end
end

Shoes.app
于 2009-04-10T14:20:42.887 回答