加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
本帖最后由 有丘直方 于 2016-7-16 10:23 编辑
解一次方程机loop do print(" 这是解一次方程机!\n 请输入方程的元的数量!\n") @unknown = gets.to_f.to_i if !@unknown.between?(1, 3) print(" 元的数量必须少于4且为正整数!\n\n") next end print(" ax = b\n 请输入常数的值:\n a = \n b = \n 输入格式为“[a, b]”。\n") if @unknown == 1 print(" a1x + b1y = c1\n a2x + b2y = c2\n 请输入常数的值:\n a1 = a2 = \n b1 = b2 = \n c1 = c2 = \n 输入格式为“[a1, a2, b1, b2, c1, c2]”。\n") if @unknown == 2 print(" a1x + b1y + c1z = d1\n a2x + b2y + c2z = d2\n a3x + b3y + c3z = d3\n 请输入常数的值:\n a1 = a2 = a3 = \n b1 = b2 = b3 = \n c1 = c2 = c3 = \n d1 = d2 = d3 = \n 输入格式为“[a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3]”。\n") if @unknown == 3 begin @ary = eval(gets) rescue print(" 非法的输入!\n\n") next end if @ary.class != Array print(" 非法的输入!\n\n") next end begin @result = [@ary[1] / @ary[0].to_f] if @unknown == 1 @result = [((@ary[4] * @ary[3] - @ary[5] * @ary[2]) / (@ary[0] * @ary[3] - @ary[1] * @ary[2]).to_f), ((@ary[0] * @ary[5] - @ary[1] * @ary[4]) / (@ary[0] * @ary[3] - @ary[1] * @ary[2]).to_f)] if @unknown == 2 @result = [((@ary[9] * @ary[4] * @ary[8] + @ary[10] * @ary[5] * @ary[6] + @ary[11] * @ary[3] * @ary[7] - @ary[9] * @ary[5] * @ary[7] - @ary[10] * @ary[3] * @ary[8] - @ary[11] * @ary[4] * @ary[6]) / (@ary[0] * @ary[4] * @ary[8] + @ary[1] * @ary[5] * @ary[6] + @ary[2] * @ary[3] * @ary[7] - @ary[0] * @ary[5] * @ary[7] - @ary[1] * @ary[3] * @ary[8] - @ary[2] * @ary[4] * @ary[6]).to_f), ((@ary[0] * @ary[10] * @ary[8] + @ary[1] * @ary[11] * @ary[6] + @ary[2] * @ary[9] * @ary[7] - @ary[0] * @ary[11] * @ary[7] - @ary[1] * @ary[9] * @ary[8] - @ary[2] * @ary[10] * @ary[6]) / (@ary[0] * @ary[4] * @ary[8] + @ary[1] * @ary[5] * @ary[6] + @ary[2] * @ary[3] * @ary[7] - @ary[0] * @ary[5] * @ary[7] - @ary[1] * @ary[3] * @ary[8] - @ary[2] * @ary[4] * @ary[6]).to_f), ((@ary[0] * @ary[4] * @ary[11] + @ary[1] * @ary[5] * @ary[9] + @ary[2] * @ary[3] * @ary[10] - @ary[0] * @ary[5] * @ary[10] - @ary[1] * @ary[3] * @ary[11] - @ary[2] * @ary[4] * @ary[9]).to_f)] if @unknown == 3 rescue print(" 非法的输入!\n\n") next end print(" 解为\n") print(" x = #{@result[0]}\n") if @unknown >= 1 print(" y = #{@result[1]}\n") if @unknown >= 2 print(" z = #{@result[2]}\n") if @unknown >= 3 print(" 谢谢使用解一次方程机!\n\n") end
loop do
print(" 这是解一次方程机!\n 请输入方程的元的数量!\n")
@unknown = gets.to_f.to_i
if !@unknown.between?(1, 3)
print(" 元的数量必须少于4且为正整数!\n\n")
next
end
print(" ax = b\n 请输入常数的值:\n a = \n b = \n 输入格式为“[a, b]”。\n") if @unknown == 1
print(" a1x + b1y = c1\n a2x + b2y = c2\n 请输入常数的值:\n a1 = a2 = \n b1 = b2 = \n c1 = c2 = \n 输入格式为“[a1, a2, b1, b2, c1, c2]”。\n") if @unknown == 2
print(" a1x + b1y + c1z = d1\n a2x + b2y + c2z = d2\n a3x + b3y + c3z = d3\n 请输入常数的值:\n a1 = a2 = a3 = \n b1 = b2 = b3 = \n c1 = c2 = c3 = \n d1 = d2 = d3 = \n 输入格式为“[a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3]”。\n") if @unknown == 3
begin
@ary = eval(gets)
rescue
print(" 非法的输入!\n\n")
next
end
if @ary.class != Array
print(" 非法的输入!\n\n")
next
end
begin
@result = [@ary[1] / @ary[0].to_f] if @unknown == 1
@result = [((@ary[4] * @ary[3] - @ary[5] * @ary[2]) / (@ary[0] * @ary[3] - @ary[1] * @ary[2]).to_f), ((@ary[0] * @ary[5] - @ary[1] * @ary[4]) / (@ary[0] * @ary[3] - @ary[1] * @ary[2]).to_f)] if @unknown == 2
@result = [((@ary[9] * @ary[4] * @ary[8] + @ary[10] * @ary[5] * @ary[6] + @ary[11] * @ary[3] * @ary[7] - @ary[9] * @ary[5] * @ary[7] - @ary[10] * @ary[3] * @ary[8] - @ary[11] * @ary[4] * @ary[6]) / (@ary[0] * @ary[4] * @ary[8] + @ary[1] * @ary[5] * @ary[6] + @ary[2] * @ary[3] * @ary[7] - @ary[0] * @ary[5] * @ary[7] - @ary[1] * @ary[3] * @ary[8] - @ary[2] * @ary[4] * @ary[6]).to_f), ((@ary[0] * @ary[10] * @ary[8] + @ary[1] * @ary[11] * @ary[6] + @ary[2] * @ary[9] * @ary[7] - @ary[0] * @ary[11] * @ary[7] - @ary[1] * @ary[9] * @ary[8] - @ary[2] * @ary[10] * @ary[6]) / (@ary[0] * @ary[4] * @ary[8] + @ary[1] * @ary[5] * @ary[6] + @ary[2] * @ary[3] * @ary[7] - @ary[0] * @ary[5] * @ary[7] - @ary[1] * @ary[3] * @ary[8] - @ary[2] * @ary[4] * @ary[6]).to_f), ((@ary[0] * @ary[4] * @ary[11] + @ary[1] * @ary[5] * @ary[9] + @ary[2] * @ary[3] * @ary[10] - @ary[0] * @ary[5] * @ary[10] - @ary[1] * @ary[3] * @ary[11] - @ary[2] * @ary[4] * @ary[9]).to_f)] if @unknown == 3
rescue
print(" 非法的输入!\n\n")
next
end
print(" 解为\n")
print(" x = #{@result[0]}\n") if @unknown >= 1
print(" y = #{@result[1]}\n") if @unknown >= 2
print(" z = #{@result[2]}\n") if @unknown >= 3
print(" 谢谢使用解一次方程机!\n\n")
end
和两位数加减法小游戏def game first_num = rand(100) next_num = rand(100) symbol = rand(2) case symbol when 0 symbol = '+' when 1 symbol = '-' end sum = "#{first_num}#{symbol}#{next_num}" print(" #{sum}是多少?\n") if gets.include?(eval(sum).to_s) @red_flowers += 1 print(" 正确!奖励你#{1}朵小红花!\n") else print(" 错误!正确答案是#{eval(sum)}!\n") end print(" 你已经有了#{@red_flowers}朵小红花!\n 说“下一题”继续游戏。\n") end print(" 两位数加减法算数游戏即将开始!\n 说“开始”进行游戏。\n") if gets.include?("开始") @red_flowers = 0 game end while gets.include?("下一题") game end
def game
first_num = rand(100)
next_num = rand(100)
symbol = rand(2)
case symbol
when 0
symbol = '+'
when 1
symbol = '-'
end
sum = "#{first_num}#{symbol}#{next_num}"
print(" #{sum}是多少?\n")
if gets.include?(eval(sum).to_s)
@red_flowers += 1
print(" 正确!奖励你#{1}朵小红花!\n")
else
print(" 错误!正确答案是#{eval(sum)}!\n")
end
print(" 你已经有了#{@red_flowers}朵小红花!\n 说“下一题”继续游戏。\n")
end
print(" 两位数加减法算数游戏即将开始!\n 说“开始”进行游戏。\n")
if gets.include?("开始")
@red_flowers = 0
game
end
while gets.include?("下一题")
game
end
具体有什么用放进ruby运行之后就知道了。。 |