首页 > 解决方案 > 如何使用该程序从命令行获取用户输入?

问题描述

我在这个程序中使用了 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 那行不通

标签: ruby

解决方案


ARGV是一个数组。你必须得到它的第一个元素。

print ARGV  # An array
x = ARGV[0]
print x

ruby filename.rb 123ARGV["123"]x"123"


推荐阅读