当前位置:  开发笔记 > 编程语言 > 正文

在Ruby Shoes GUI ToolKit中循环

如何解决《在RubyShoesGUIToolKit中循环》经验,为你挑选了1个好方法。

我现在只使用鞋子几天,所以也许我错过了一些东西.我给儿子写了一个小程序来帮助他学习乘法表.当他得到十个正确时,他已经完成了.我似乎无法使用SHOES获得while循环.有些人请告诉我一些声明.当我尝试; 它要么擦掉我的流程,要么堆叠语句或鞋子崩溃.

提前致谢.山姆



1> Pesto..:

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

或者,我认为解决方案更有趣,使用urlvisit:

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

推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有