While在Ruby中循环并转换为函数
我正在努力学习Ruby 额外学分练习1要求: 将此while循环转换为可以调用的函数,并替换为6 在测试中(i<6)使用一个变量 守则:While在Ruby中循环并转换为函数,ruby,function,loops,while-loop,learn-ruby-the-hard-way,Ruby,Function,Loops,While Loop,Learn Ruby The Hard Way,我正在努力学习Ruby 额外学分练习1要求: 将此while循环转换为可以调用的函数,并替换为6 在测试中(i
i = 0
numbers = []
while i < 6
puts "At the top i is #{i}"
numbers.push(i)
i = i + 1
puts "Numbers now: #{numbers}"
puts "At the bottom i is #{i}"
end
puts "The numbers: "
for num in numbers
puts num
end
i=0
数字=[]
当我<6
将“在顶部,我是#{i}”
数字推送(一)
i=i+1
放置“现在的数字:#{Numbers}”
在底部写上“我是#{i}”
终止
把“数字:
对于数字中的num
放置num
终止
我的尝试:
i = 0
numbers = []
def loops
while i < 6
puts "At the top i is #{i}"
numbers.push(i)
i = i + 1
puts "Numbers now: #{numbers}"
puts "At the bottom i is #{i}"
end
end
loops
puts "The numbers: "
for num in numbers
puts num
end
i=0
数字=[]
def循环
当我<6
将“在顶部,我是#{i}”
数字推送(一)
i=i+1
放置“现在的数字:#{Numbers}”
在底部写上“我是#{i}”
终止
终止
循环
把“数字:
对于数字中的num
放置num
终止
如你所见,我已经尝试将块变成一个函数,但还没有将6变成一个变量
错误:
ex33.rb:5:in `loops': undefined local variable or method `i' for main:Object (Na
meError)
from ex33.rb:15:in `<main>'
from ex33.rb:15:in `<main>'
ex33.rb:5:in'loops:未定义的局部变量或main:Object(Na)的方法'i'
米罗)
从ex33.rb:15:in`'
从ex33.rb:15:in`'
我做错了什么
编辑:好的,稍微改进一下。现在数字变量超出范围
def loops (i, second_number)
numbers = []
while i < second_number
puts "At the top i is #{i}"
i = i + 1
numbers.push(i)
puts "Numbers now: #{numbers}"
puts "At the bottom i is #{i}"
end
end
loops(0,6)
puts "The numbers: "
for num in numbers
puts num
end
def循环(i,第二个_编号)
数字=[]
而我是第二个
将“在顶部,我是#{i}”
i=i+1
数字推送(一)
放置“现在的数字:#{Numbers}”
在底部写上“我是#{i}”
终止
终止
循环(0,6)
把“数字:
对于数字中的num
放置num
终止
当你说def
时,i
超出范围。该方法无法“看到”它。使用@i
代替(@赋予变量更大的“可见性”),或者在方法内部移动i=6
,或者找出如何使用方法参数。当您说def
时,i
超出范围。该方法无法“看到”它。使用@i
代替(@赋予变量更大的“可见性”),或者在方法内部移动i=6
,或者找出如何在方法中使用参数。正如@steenslag所说,i
超出循环的范围。我不建议切换到使用@I
,因为I
仅由循环使用
您的函数是一个可用于生成数字数组的实用程序。该函数使用i
来计算它到底走了多远(但函数的调用者并不关心这一点,它只需要得到的数字
)。该函数还需要返回数字
,因此也要将其移动到循环中
def loops
i = 0
numbers = []
while i < 6
puts "At the top i is #{i}"
numbers.push(i)
i = i + 1
puts "Numbers now: #{numbers}"
puts "At the bottom i is #{i}"
end
end
def循环
i=0
数字=[]
当我<6
将“在顶部,我是#{i}”
数字推送(一)
i=i+1
放置“现在的数字:#{Numbers}”
在底部写上“我是#{i}”
终止
终止
您现在必须考虑这样一个事实,循环
的调用者不再能看到数字
。祝你学习顺利。正如@steenslag所说,i
超出了循环的范围。我不建议切换到使用@I
,因为I
仅由循环使用
您的函数是一个可用于生成数字数组的实用程序。该函数使用i
来计算它到底走了多远(但函数的调用者并不关心这一点,它只需要得到的数字
)。该函数还需要返回数字
,因此也要将其移动到循环中
def loops
i = 0
numbers = []
while i < 6
puts "At the top i is #{i}"
numbers.push(i)
i = i + 1
puts "Numbers now: #{numbers}"
puts "At the bottom i is #{i}"
end
end
def循环
i=0
数字=[]
当我<6
将“在顶部,我是#{i}”
数字推送(一)
i=i+1
放置“现在的数字:#{Numbers}”
在底部写上“我是#{i}”
终止
终止
您现在必须考虑这样一个事实,循环
的调用者不再能看到数字
。祝你学习顺利。我可能误读了“转换while循环”,但我的解决方案是:
def loop(x, y)
i = 0
numbers = []
while i < y
puts "At the top i is #{i}"
numbers.push(i)
i += 1
puts "Numbers now: ", numbers
puts "At the bottom i is #{i}"
end
puts "The numbers: "
# remember you can write this 2 other ways?
numbers.each {|num| puts num }
end
loop(1, 6)
def循环(x,y)
i=0
数字=[]
而我
将“在顶部,我是#{i}”
数字推送(一)
i+=1
现在输入“数字:”,数字
在底部写上“我是#{i}”
终止
把“数字:
#还记得你可以用另外两种方式写吗?
numbers.each{| num | put num}
终止
循环(1,6)
我可能误读了“转换while循环”,但我的解决方案是:
def loop(x, y)
i = 0
numbers = []
while i < y
puts "At the top i is #{i}"
numbers.push(i)
i += 1
puts "Numbers now: ", numbers
puts "At the bottom i is #{i}"
end
puts "The numbers: "
# remember you can write this 2 other ways?
numbers.each {|num| puts num }
end
loop(1, 6)
def循环(x,y)
i=0
数字=[]
而我
将“在顶部,我是#{i}”
数字推送(一)
i+=1
现在输入“数字:”,数字
在底部写上“我是#{i}”
终止
把“数字:
#还记得你可以用另外两种方式写吗?
numbers.each{| num | put num}
终止
循环(1,6)
我刚刚提出了一个带有参数的函数,它也用一个变量替换了6,我用代码编辑了OP。然而,正如你所说,现在“数字”已经看不见了……我刚刚提出了一个带有参数的函数,它也用一个变量替换了6,我用代码编辑了OP。然而,正如你所说,现在“数字”已经看不见了。。。