LoginSignup
0
1

More than 5 years have passed since last update.

Ruby > ズンドコキヨシ by Ruby入門者

Last updated at Posted at 2016-12-24

Rubyの文法を入門するには、今ブームのズンドコキヨシしかないかと思われる。 

v0.1 (間違いあり: doko表示抜け)

ideoneで実装してみました。
http://ideone.com/oV6zKW

zncnt = 0

for idx in 1..100
  elem = [*1..10].sample
  mdl = elem.modulo(2)
#  print(idx, ",", elem, ",", mdl, "\n")
  if mdl == 0 then
    zncnt = zncnt + 1
    print("zun,")
  else
    if zncnt == 4 then
        print("doko,KIYOSHI!\n")
        break
    else 
        zncnt = 0
    end
  end
end
結果
Success time: 0.02 memory: 9792 signal:0
zun
zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,zun,doko,KIYOSHI!

学習内容

  • for文
  • sampleによる乱数の取り方
  • 剰余の取り方
  • if文
  • print文
  • break文

参考 http://qiita.com/Konboi@github/items/e53296e99e297f83cfd1
参考 http://ref.xaio.jp/ruby/classes/array
参考 http://www.rubylife.jp/ini/numeric_class/index6.html
参考 http://www.rubylife.jp/ini/for/index9.html
参考 http://www.task-notes.com/entry/20141109/1415520719
参考 http://www.task-notes.com/entry/20141117/1416153598

v0.2 doko表示追加

@mattnさんに指摘いただいた間違いを修正いたしました。
情報感謝です。

=begin
v0.2 2016/12/24
  - fix bug: did not show 'doko,' when the condition is not right
v0.1 2016/12/24
  - first code to print zundoko
=end

zncnt = 0

for idx in 1..100
  elem = [*1..10].sample
  mdl = elem.modulo(2)
#  print(idx, ",", elem, ",", mdl, "\n")
  if mdl == 0 then
    zncnt = zncnt + 1
    print("zun,")
  else
    if zncnt == 4 then
        print("doko,KIYOSHI!\n")
        break
    else 
        print("doko,")
        zncnt = 0
    end
  end
end
結果
doko,zun,doko,zun,zun,doko,zun,zun,zun,doko,doko,doko,doko,doko,zun,zun,zun,zun,doko,KIYOSHI!

上記ではバージョンコメントを追加しました。
コメントの書き方は以下を参考にし、=beginを使いました。
http://qiita.com/Mocacamo/items/318b193ded19fd37ffd6

0
1
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1