用户输入一个数字,该数字将在数组中搜索。之后,他/她将输入数组的大小和元素。在编写解决方案时:
number_to_be_searched = gets.chomp #my input=>4
size_of_array = gets.chomp #input => 6
array = Array.new
Integer(size_of_array).times do
str = gets
str = Integer(str)
array.push(str)
end
print array
if array.include? Integer(number_to_be_searched)
print array.index(number_to_be_searched)
end
我收到以下错误:
:Integer()的无效值“1 4 5 7 9 12”(ArgumentError)
答案 0 :(得分:0)
print 'Number to be searched: '
number_to_be_searched = gets.chomp
print 'Enter size of array: '
size_of_array = gets.chomp
array = Array.new
Integer(size_of_array).times do |i|
print "Enter #{i+1}th number: "
str = gets
str = Integer(str)
array.push(str)
end
p "Numbers you Entered are #{array}"
if array.include? Integer(number_to_be_searched)
p "Index of found number is : #{array.index(number_to_be_searched.to_i)}"
end