用Ruby给小宝做的数学算术口算练习程序(带四则运算的!)

上文中的《用Python给小宝做的数学算术口算练习程序》

Python的那段程序只包含 加+ 减- 运算,

本文 用Ruby 重新做了(当然包含了+-*/加减乘除……


为啥用 Ruby重新做……? 因为我忍受不了 Python的缩进,太不灵活……

我就是喜欢来回来去的拷贝、粘贴代码……但在Python里 一但拷贝粘贴代码,代码的缩进容易搞不好……代码一乱,我就不会搞规矩、整齐了。

看来在Python里……“缩进”要了我的命。


不得以改为Ruby , 到目前为止,Ruby给了我个惊喜。



inyn=1
char=49
timu_nums=10

until inyn=='n' do
  righ1t=0
  flagwrong=0 #没错过

  if inyn=='0'
    break
  end
#           inyn=gets
      print '一共有:', timu_nums,"道题目。" ,"\n" #\n回车
      num=0
           while num<timu_nums do
             #here11:
             flagwrong=0 #默认题目是没错
             num=num+1
             print '第',num, '题:'

             fuhao=1+rand(5) #4)  #0跳过 1 加法 2 减法
             print '运算为:', fuhao,"\n"
             x=rand(20)
             y=rand(10)
             case fuhao
               when 1
                  print x,'+',y,"="
                  z=gets.chomp.to_i

                if (x+y==z) then
                  print "正确! ",x,"+",y, "就是等于"
                  puts z
                else
                    10.times  {
                      flagwrong=1
                      puts "不对啊!再算一遍!!"
                      print x,'+',y,"="
                      z=gets.chomp.to_i
                        if (x+y==z) then
                          break
                        end
                    }
                end

               when 2   #这次(算减法) 优化一下分支结构…
                 if (x<y) then
                   temp=x;x=y;y=temp  #如果原 被减数x 小于 减数y 则交换x,y这两个值
                 end
                 print x, '-', y, "="
                 z=gets.chomp.to_i

                    until ( (x-y)==z) do
                      flagwrong=1
                      puts "不对啊!再算一遍!!"
                      print x, '-', y, "="
                      z=gets.chomp.to_i
                      if (x-y==z) then
                        break
                      end
                    end#until (x-y=z)

                    print "正确! ", x,"-", y, "就是等于"
                    print z
                    puts "!!"

                when 3 #乘法
                 #else
                 print x, '*', y, "="
                 z=gets.chomp.to_i

                 until ( (x*y)==z) do
                   flagwrong=1
                   puts "不对啊!再算一遍!!"
                   print x, '*', y, "="
                   z=gets.chomp.to_i
                   if (x*y==z) then #这个 if  语句 没用了!?
                     break
                   end
                 end#until (x*y=z)

                 print "正确! ", x,"*", y, "就是等于"
                 print z
                 puts "!!"

               else # 除法等
                    if (x<y) then
                      temp=x;x=y;y=temp  #如果原 被数x 小于 除数y 则交换x,y这两个值
                    end
                 print x, '/', y, "=商为:"
                 z=gets.chomp.to_i
                    if (y==0) then
                        print "\n 出错了? 想一想除数(第2个数)可以为0吗?"
                        puts "重新出题吗?按 y 键重新出题..."

                        inyn2=gets.chomp.to_s
                        if (inyn2=='y') then
                            #goto here11
                            continue
                        end
                      end
                    print ",余数为:"
                    z2=gets.chomp.to_i

                 until ( x==(z*y+z2) ) do
                   flagwrong=1
                   puts "不对啊!再算一遍!!"
                   print x, '/', y, "=商为:"
                   z=gets.chomp.to_i
                   print ",余数为:"
                   z2=gets.chomp.to_i
                   if ( x==(z*y+z2) ) then
                     break
                   end
                 end#until (x-y=z)

                 print "正确! ",x, "/",y,"的商就是",z," 余数为:",z2
                 puts "!!"


                  end #cass fuhao


             #puts num第?题了
             puts

                if (flagwrong==0) then #本题没错过(一次就做对)那成绩加+1咯!
                    righ1t=righ1t+1
                end
             end


           #end

  print "刚才一共做对了", righ1t, "/", timu_nums, "道题目。","\n"
  puts "请按任意键继续,或者按'n'(小写)退出!"
#puts inyn #是否是退出的 n
  inyn=gets.chomp.to_s
 # char=getchar
end


程序没有搞成 OO的哭

没用到类,请高手指点吧。

阅读更多
文章标签: ruby python 优化 oo
个人分类: 个人随手记
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭