我是Ruby的新手,无法弄清楚如何从用户那里获取数组的输入并显示它.如果有人能够清楚我可以添加我的逻辑来找到最大的数字.
#!/usr/bin/ruby
puts "Enter the size of the array"
n = gets.chomp.to_i
puts "enter the array elements"
variable1=Array.new(n)
for i in (0..n)
variable1[i]=gets.chomp.to_i
end
for i in (0..n)
puts variable1
end
Run Code Online (Sandbox Code Playgroud)
如何在一行中捕获数组?
#!/usr/bin/ruby
puts "Enter a list of numbers"
list = gets # Input something like "1 2 3 4" or "3, 5, 6, 1"
max = list.split.map(&:to_i).max
puts "The largest number is: #{max}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8066 次 |
| 最近记录: |