Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用Ruby使用循环查找数组中的最大整数_Arrays_Ruby_Loops_Integer - Fatal编程技术网

Arrays 使用Ruby使用循环查找数组中的最大整数

Arrays 使用Ruby使用循环查找数组中的最大整数,arrays,ruby,loops,integer,Arrays,Ruby,Loops,Integer,我需要在Ruby中使用循环找到数组中的最大整数 例:数字=[10,20,30,40,50,60] 注意:我不能使用.max函数。必须是一个循环。创建另一个变量来存储当前最大值,然后循环数字 max_num = 0 numbers.each { |n| max_num = n if n > max_num } max_num = numbers.first numbers.each { |n| max_num = n if n > max_num } 我喜欢用inject来做这样

我需要在Ruby中使用循环找到数组中的最大整数

例:数字=[10,20,30,40,50,60]


注意:我不能使用.max函数。必须是一个循环。

创建另一个变量来存储当前最大值,然后循环数字

max_num = 0
numbers.each { |n| max_num = n if n > max_num }
max_num = numbers.first 
numbers.each { |n| max_num = n if n > max_num }

我喜欢用
inject
来做这样的事情

numbers.inject(0){|acc,n| n > acc ? n : acc}

另一个要使用的是枚举


与philip yoo的答案类似,但适用于负数数组

max_num = 0
numbers.each { |n| max_num = n if n > max_num }
max_num = numbers.first 
numbers.each { |n| max_num = n if n > max_num }

此代码在
for
循环上运行,而不是
。每个
。根据您的提问,我认为您可能需要一个实际的循环构造函数

numbers = [10, 20, 30, 40, 50]
greatest_num = numbers.first
for n in numbers
    greatest_num = n if n > greatest_num
end
p greatest_num

不是循环,但可以使用递归

numbers = [10, 20, 30, 40, 60, 50]

def biggest(arr)
  return arr.first if arr.size == 1
  cand = biggest(arr[1..-1])
  arr.first > cand ? arr.first : cand
end

biggest(numbers) #=> 60
两周前,一种更为“新手”的方式(从我是一名:-)开始了Ruby之旅

numbers = [10, 20, 30, 40, 50, 60] # your array
max_num = numbers[0] # Setting max_num to the first element of your array    
numbers.size.times{|x| if max_num < numbers[x] then max_num = numbers[x] end} # I use 
 #* the .size method to find how many elements are on your array and the .times method
 #* to iterate "size" "times" over your array.  In the block you have a simple comparison
puts max_num #displaying the result
number=[10,20,30,40,50,60]#你的数组
max_num=numbers[0]#将max_num设置为数组的第一个元素
numbers.size.times{| x |如果max_num
所以,再详细一点。。。。数组索引始终从0开始。我们从不为max_num变量赋值零,因为数组可能包含负数

max_num = 0
numbers.each { |n| max_num = n if n > max_num }
max_num = numbers.first 
numbers.each { |n| max_num = n if n > max_num }
数字、大小、倍数。。。在Ruby中,我们可以对一个对象使用多个方法,在我们的示例中,对象是数组数。因此,numbers.size将返回6(记住数组从0开始。因此从0到5等于数组中的6个元素),然后我们使用.times方法循环6次块

在街区内。times方法将0到5之间的值传递给变量x。所以,剩下的就是做一个简单的比较。如果max_num小于x位置的numbers数组(numbers[x]),则将numbers[x]设为我的新max_num(max_num=numbers[x])。块将自身重复6次:-)


代码是有效的。。至于解释。。这就是我对它的理解。。。由于英语不是我的母语,我要求社区对代码和解释进行评论(好的和坏的)

如果你要发布你的作业,至少告诉我们你到目前为止尝试了什么,以及你遇到了什么问题。我投票将这个问题作为离题题来结束,因为它看起来像是一个家庭作业练习,没有任何尝试去解决它。
,while
直到
使用循环,但带块的枚举器不使用循环。如果您不打算排除后者,您可能希望删除全部大写的句子。我假设整个
max
方法家族都是禁止使用的(`max_by,minmax,等等)。那么
arr=numbers.uniq;(arr-arr.min(arr.size-1))。首先,#=>60
?@CarySwoveland,看一看-特别是第3点。捕捉得好
max_num
应该设置为数组中的第一个数字,而不是0:)…或者设置为
-Float::INFINITY#=>-INFINITY
(与
-1.0/0
相同)。请参阅@Cappy的文章,以了解此的修订版本:)您和@Cappy都应该添加
max_num
,作为第三行,检索值。
reduce
inject
更可取,@danielrsmith在其早期回答中使用了
reduce
inject
都做了相同的事情(reduce是inject的别名),但根据它的说法,首选使用reduce。我意识到这一点。我只是不明白你的答案有什么意义,因为它是先前答案的重复。