我想通過讓程序工作來理解嵌套循環。如果可能的話,我想使用循環的「每個做法」。現在循環執行所有的第一個循環,然後第二個......等等......我想要做的是,執行第一個循環1次,然後一次下降到第二個循環......等等。這裏是我的代碼(粘貼以下)Ruby嵌套每個循環
期望輸出會是這樣的:
index 3.0 3.0
+-------------------------------------------------------+
0 -23.4 -23.4
1 -2226.74 -2226.74
2 -1.93464e+07 -1.93464e+07
代碼
class LogisticsFunction
puts "Enter two numbers, both between 0 and 1 (example: .25 .55)."
puts "After entering the two numbers tell us how many times you"
puts "want the program to iterate the values (example: 1)."
puts "Please enter the first number: "
num1 = gets.chomp
puts "Enter your second number: "
num2 = gets.chomp
puts "Enter the number of times you want the program to iterate: "
iter = gets.chomp
print "index".rjust(1)
print num1.rjust(20)
puts num2.rjust(30)
puts "+-------------------------------------------------------+"
(1..iter.to_i).each do |i|
print i
end
(1..iter.to_i).each do |i|
num1 = (3.9) * num1.to_f * (1-num1.to_f)
print num1
end
(1..iter.to_i).each do |i|
num2 = (3.9) * num2.to_f * (1-num2.to_f)
print num2
end
end
你只希望這在給定的方式顯示? –