ruby - 如何使用该程序从命令行获取用户输入?
问题描述
我在这个程序中使用了 Ruby,它基本上计算了你需要多少个 5 和 3 来获得输入量。现在我正在处理一个问题,当我必须从 cmd 输入时,我可以让它工作,例如ruby filename.rb 123
begin
x = Integer(gets.chomp)
rescue
puts "Please input an integer"
retry
end
y = 5
atlikums = x % y
if x % 10 == 1
puts " #{(x / 5) - 1} five cent, 2 three cent "
elsif x > 100 && x % 10 == 5
puts " #{x / 5} five cent "
elsif x < 8
puts "Minimal amount is 8 cents"
elsif x==9
puts "#{x/3} three cents"
elsif atlikums == 0
puts " #{x / 5} five cents"
elsif atlikums == 1
puts " #{(x / 5) - 1} five cents, 2 three cent"
elsif atlikums == 2
puts " #{(x / 5) - 2} five cents, 4 three cent "
elsif atlikums == 3
puts " #{x/ 5} five cents, 1 three cent"
elsif atlikums == 4
puts " #{x/ 5} five cents, 3 three cent"
end
我已经尝试过x = ARGV
了,但 idk 那行不通
解决方案
ARGV
是一个数组。你必须得到它的第一个元素。
print ARGV # An array
x = ARGV[0]
print x
与ruby filename.rb 123
,ARGV
是["123"]
和x
是"123"
。
推荐阅读
- ruby - 无法在我的 Ruby 代码中将数组强制转换为 Float (TypeError)
- java - 没有 RestTemplate 的休息电话
- node.js - 需要更新 mongodb 中数组中的元素
- javascript - 是否有用于在浏览器中滚动的特殊线程?
- c++ - 实现简单的逻辑和延迟模拟器
- node.js - 在节点中将midi转换为mp3
- node.js - 使用 jwt 在 .net core 2.1 中构建网站(在 node.js 中)和 API。如何连接它们?
- symfony - 如何使用样式标记将样式应用于 VueJS 组件
- spring-boot - Spring Boot/Security - 自定义 404 页面
- java - 如何使用 querydsl 对带有列表的对象进行操作?