gpt4 book ai didi

ruby - 简化 Ruby 语句

转载 作者:数据小太阳 更新时间:2023-10-29 08:59:48 25 4
gpt4 key购买 nike

<分区>

我这里有很长的代码 w/c 是 begin-end block 和 if 语句的组合。如何在 all if 语句中简化它?

print "Put a number between 0 to 100: "

begin
numb = gets.chomp
numb = Integer(numb)
rescue
print "Please put a number not a text: "
retry
end

if numb < 0
puts "You can't enter a negative number!"
elsif numb <= 50
puts "#{numb} is between 0 and 50"
elsif numb <= 100
puts "#{numb} is between 51 and 100"
else
puts "#{numb} is above 100"
end

重构

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com