不确定我为什么会收到此错误。这是代码:
puts "Please input an integer"
puts ">>> A positive input stores how many numbers are >, < or = to 1000"
puts ">>> -1 terminates the application and outputs how many numbers were >, < or = to 1000"
num = gets.chomp
i = num.to_i
while num != "-1"
if i > 1000
greater += 1
elsif i < 1000
less += 1
elsif i == 1000
equal += 1
elsif i < 0
puts "Please only enter positive integers"
end
end
puts "Number of integers greater than 1000 = #{greater}"
puts "Number of intergers less than 1000 = #{less}"
puts "Number of integers equal to 1000 = #{equal}"
gets
代码显然是在尝试比较输入的数字以查看它们是否大于,小于或等于1000,然后将它们存储在输入-1后显示的变量中。
答案 0 :(得分:1)
您尚未初始化greater
,less
和equal
个变量。尝试添加
greater = 0
less = 0
equal = 0
或者,如果您更喜欢使用多个作业的较短版本
greater = less = equal = 0
在while循环之前