Ruby Shoes GUI ToolKit 中的 While 循环

发布于 2024-07-16 10:15:32 字数 180 浏览 10 评论 0原文

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

提前致谢。 山姆

I have only been using Shoes for a few days now so maybe I am missing something. I wrote my son an little program to help him learn his multiplication tables. When he gets ten correct he's done. I can not seem to get the while loop right using SHOES.
Would some please show me a while statement. When I try; it either wipes out my flow and stack statements or Shoe crashes.

thanks in advance.
Sam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2024-07-23 10:15:32

我不知道你是如何使用 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

或者,我认为更有趣的解决方案是使用 url 和<代码>访问:

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

I don't know how you're using a while loop. Most likely you're trying to recreate the stack each iteration through the while loop, which is a bad idea. The two solutions that spring immediately to mind are to handle the logic on a button click and keep track of the number correct in a row, like this:

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

Or, the solution which I think is much more interesting, using url and 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文