简单的程序但非常卡住- Ruby 中的循环
Simple program but so very stuck- Loops in Ruby
我必须编写一个程序,要求用户输入一个数字。
该程序不断询问用户输入数字,直到用户键入 'Stop'
此时应打印用户输入的数字总和。
我已经尝试了很多很多事情并且 none 我的想法奏效了。
这就是我所拥有的 - 但我可以认为它不正确。我做错了什么?
我只用过 while 循环和数组
total_user_input = []
# As long as the user inputs a number, the program will keep putting Give me a number
# and then adding that number to the total_user_input array.
puts "Give me a number: "
while user_input = gets.chomp.to_i
#add the input to the array total_user_input
total_user_input.push(user_input.to_i)
puts "Give me a number: "
# If the user however types stop, then the loop is broken and we jump down to the
# sum bit - where all of the numbers in the total_user_input array are added together
# and printed. End of program!
if user_input == "stop"
break
end
sum = 0
total_user_input.each { |num|
sum += num
}
puts sum
end
输出不正常。
注意以下几点:
get.chomp.to_i
会将每个输入转换为整数。 ("stop" 或任何非整数字符串将为 0)
- 流程安排的比较乱
total_user_input = []
puts "Give me a number: "
while user_input = gets.chomp.strip
total_user_input.push(user_input.to_i)
sum = 0
total_user_input.each { |num|
sum += num
}
puts sum
if user_input == "stop"
break
end
end
希望你明白这一点。
这就是我喜欢使用 loop do end
语法和 break
的方式。还添加了更多文本,以便用户知道发生了什么。
total_user_input = []
puts 'Give me a number or "stop" to end: '
loop do
user_input = gets.chomp
total_user_input << user_input.to_i
puts "Give me a number: "
break if user_input.downcase == "stop"
end
puts "Total entered: #{total_user_input.inject(&:+)}" unless total_user_input.empty?
puts 'goodbye!'
由于其他人已经发现您的代码存在问题,所以让我建议您如何重新组织代码。 Ruby 提供了多种执行循环的方法,但您发现主要依靠方法 Kernel#loop and the keyword break 是可取的。 (你会及时了解到,loop
在与枚举器一起使用时特别方便。)
def sum_numbers
tot = 0
loop do
print 'Gimme a number: '
s = gets.chomp
break if s == 'Stop'
tot += s.to_i
end
tot
end
关键字 break
可以有选择地接受一个参数(尽管我不能说文档中没有提到的原因),在这种情况下它(如果是文字)或其值(如果是变量或方法) 由 loop
编辑 return。在这里人们通常会看到
break tot if s == 'Stop'
没有最后一行,tot
。由于循环 returns tot
并且是该方法执行的最后一次计算,该方法将 return 最终值 tot
.
你可以改写
return tot if user_input == 'Stop'
但我认为大多数编码人员认为最佳实践表明,除非有充分的理由,否则不应 return 来自循环内(或嵌套循环内)的方法。
一些小点:
- 我使用
print
而不是 puts
,因为用户的输入将显示在与提示相同的行上。
- 我使用
s
(代表 "string")而不是 user_input
,因为它减少了拼写错误的可能性(例如,user_imput
)、加快阅读速度,以及 (可能是我的一个缺点),看起来更整洁。没错,s
不是描述性的,但只需记住连续三行代码的含义即可。其他人可能不同意。
- 你可以写
break if s.downcase == 'stop'
如果你想 'stop'
或 'STOP'
具有与 'Stop'
相同的效果。
'23O3'.to_i #=> 23
(这是一个哦,不是零),所以在现实生活中你想要确认输入了 'Stop'
或数字的字符串表示形式。
我必须编写一个程序,要求用户输入一个数字。 该程序不断询问用户输入数字,直到用户键入 'Stop' 此时应打印用户输入的数字总和。 我已经尝试了很多很多事情并且 none 我的想法奏效了。 这就是我所拥有的 - 但我可以认为它不正确。我做错了什么?
我只用过 while 循环和数组
total_user_input = []
# As long as the user inputs a number, the program will keep putting Give me a number
# and then adding that number to the total_user_input array.
puts "Give me a number: "
while user_input = gets.chomp.to_i
#add the input to the array total_user_input
total_user_input.push(user_input.to_i)
puts "Give me a number: "
# If the user however types stop, then the loop is broken and we jump down to the
# sum bit - where all of the numbers in the total_user_input array are added together
# and printed. End of program!
if user_input == "stop"
break
end
sum = 0
total_user_input.each { |num|
sum += num
}
puts sum
end
输出不正常。
注意以下几点:
get.chomp.to_i
会将每个输入转换为整数。 ("stop" 或任何非整数字符串将为 0)- 流程安排的比较乱
total_user_input = []
puts "Give me a number: "
while user_input = gets.chomp.strip
total_user_input.push(user_input.to_i)
sum = 0
total_user_input.each { |num|
sum += num
}
puts sum
if user_input == "stop"
break
end
end
希望你明白这一点。
这就是我喜欢使用 loop do end
语法和 break
的方式。还添加了更多文本,以便用户知道发生了什么。
total_user_input = []
puts 'Give me a number or "stop" to end: '
loop do
user_input = gets.chomp
total_user_input << user_input.to_i
puts "Give me a number: "
break if user_input.downcase == "stop"
end
puts "Total entered: #{total_user_input.inject(&:+)}" unless total_user_input.empty?
puts 'goodbye!'
由于其他人已经发现您的代码存在问题,所以让我建议您如何重新组织代码。 Ruby 提供了多种执行循环的方法,但您发现主要依靠方法 Kernel#loop and the keyword break 是可取的。 (你会及时了解到,loop
在与枚举器一起使用时特别方便。)
def sum_numbers
tot = 0
loop do
print 'Gimme a number: '
s = gets.chomp
break if s == 'Stop'
tot += s.to_i
end
tot
end
关键字 break
可以有选择地接受一个参数(尽管我不能说文档中没有提到的原因),在这种情况下它(如果是文字)或其值(如果是变量或方法) 由 loop
编辑 return。在这里人们通常会看到
break tot if s == 'Stop'
没有最后一行,tot
。由于循环 returns tot
并且是该方法执行的最后一次计算,该方法将 return 最终值 tot
.
你可以改写
return tot if user_input == 'Stop'
但我认为大多数编码人员认为最佳实践表明,除非有充分的理由,否则不应 return 来自循环内(或嵌套循环内)的方法。
一些小点:
- 我使用
print
而不是puts
,因为用户的输入将显示在与提示相同的行上。 - 我使用
s
(代表 "string")而不是user_input
,因为它减少了拼写错误的可能性(例如,user_imput
)、加快阅读速度,以及 (可能是我的一个缺点),看起来更整洁。没错,s
不是描述性的,但只需记住连续三行代码的含义即可。其他人可能不同意。 - 你可以写
break if s.downcase == 'stop'
如果你想'stop'
或'STOP'
具有与'Stop'
相同的效果。 '23O3'.to_i #=> 23
(这是一个哦,不是零),所以在现实生活中你想要确认输入了'Stop'
或数字的字符串表示形式。