如何在ruby中创建终端计算器程序?

如何在ruby中创建终端计算器程序?,ruby,Ruby,到目前为止,我已经 puts "Enter a calculation: " calc = gets.chomp def add(a, b) puts a + b end puts add(calc) 现在我很惭愧地承认,但我被难住了,我试着写添加方法等等。。。但我似乎无法集中精力来计算并输出正确的结果 为了简化这一点,我如何获得添加工作的能力 即用户输入计算(2个整数),程序添加 计算,程序输出结果,程序要求另一个 计算 一步一步地思考你的问题。您希望用户提供给整数。因此,从一个

到目前为止,我已经

puts "Enter a calculation: "

calc = gets.chomp

def add(a, b)
   puts a + b
end

puts add(calc)
现在我很惭愧地承认,但我被难住了,我试着写添加方法等等。。。但我似乎无法集中精力来计算并输出正确的结果

为了简化这一点,我如何获得添加工作的能力

即用户输入计算(2个整数),程序添加 计算,程序输出结果,程序要求另一个 计算


一步一步地思考你的问题。您希望用户提供给整数。因此,从一个简单的提示开始,就像您已经做的那样:

puts "Enter your first value"
现在从用户处获取值:

firstVal = gets.chomp
现在提供另一个提示,并获取第二个值

puts "Enter your second value"
secVal = gets.chomp
并输出您的结果:

puts "#{firstVal} + #{secVal} = #{firstVal.to_i + secVal.to_i}"
有时候,简单明了地写出来是最容易的第一步。现在您可以创建一个add函数来更有效地执行此操作。试试看,看看你有没有运气

编辑: 另外,我注意到您的
add
函数需要两个参数,但您只传递了一个参数。 为了使用两个参数调用函数,您需要两个值来提供它。例如:

x = 5
y = 2

def add(a, b)
    return a + b
end

puts add(x, y)

这里有一个快速的小计算器,我为你准备的,可以帮助你开始:

#! /usr/bin/ruby

def add(a, b)
  a + b
end

while(true) do
  puts "Enter a calculation: "

  # chomp off the \n at the end of the input
  calc = gets.chomp

  # quit if the user types exit
  exit if calc.include?("exit")

  # puts the result of the add function, taking input of calc "split" into two numbers
  puts add(calc.split("").first.to_i, calc.split("").last.to_i)
  # spacing
  puts
end

以此为基础。如果您想继续接收计算请求,可以将流程放入循环(在许多解决方案中)


我认为这种脚本非常适合作为案例陈述。下面是使用二进制运算符的第一个过程:

#! /usr/bin/ruby

def calc
  puts "Calculator 1.0 \nEnter 'q' to quit."

  while true
    print ">> "
    str = gets.chomp.split(" ")  # splits into array, rejects blanks
    return if str[0] == 'q'      # quit if first element is 'q'

    operand1 = str[0].to_i
    operand2 = str[2].to_i
    operator = str[1].to_sym

    case operator
    when :+ then puts operand1 + operand2
    when :- then puts operand1 - operand2
    when :* then puts operand1 * operand2
    when :/ then puts operand1 / operand2
    when :% then puts operand1 % operand2
    else
      puts "invalid input"
    end
  end

end

if __FILE__ == $0     # if run as script, start calc: see http://j.mp/HOTGq8
  calc
end
然后,在命令行:

$ ./calc.rb 
Calculator 1.0 
Enter 'q' to quit.
>> 55 / 5
11
>> 90 / 10
9
>> 5 * 3093
15465
祝你好运


如果你刚开始的话,这些都是很好的资源:

我知道这是一篇比较老的文章,但是人们仍然能找到这个答案,我想补充一下jkrmr上面所说的

jkrmr发布的代码很棒,但不处理浮点计算,这是一个简单的修复方法,因此我添加了该函数性。:-)


你为什么不告诉我们你试过什么,并询问一些令人困惑的具体部分呢?:)完成:)但很尴尬。没什么好尴尬的-每个人都从某个地方开始。:)你认为问题出在哪里?谢谢CDub,Keikoku,还有一个,add方法接受两个参数,calc只有一个。我只是不知道如何使用add来计算用户输入量Hank you adback03,有时我会把事情弄得过于复杂:)别担心。编程当然不容易,尤其是当你刚刚开始的时候。祝你好运谢谢CDub,这非常有用。这太棒了!谢谢jmromer
$ ./calc.rb 
Calculator 1.0 
Enter 'q' to quit.
>> 55 / 5
11
>> 90 / 10
9
>> 5 * 3093
15465
#! /usr/bin/ruby

def calc
  puts "Calculator 1.1 \nEnter 'q' to quit."

  while true
    print ">> "
    str = gets.chomp.split(" ")  # splits into array, rejects blanks
    return if str[0] == 'q'      # quit if first element is 'q'
    if str[0].include? "."
      operand1 = str[0].to_f
    else
      operand1 = str[0].to_i
    end

    operator = str[1].to_sym

    if str[2].include? "."
      operand2 = str[2].to_f
    else
      operand2 = str[2].to_i
    end

    case operator
    when :+ then puts operand1 + operand2
    when :- then puts operand1 - operand2
    when :* then puts operand1 * operand2
    when :/ then puts operand1 / operand2
    when :% then puts operand1 % operand2
    else
      puts "invalid input"
    end
  end

end

if __FILE__ == $0     # if run as script, start calc: see http://j.mp/HOTGq8
  calc
end