除以 ruby 的一半来创建一个有效的计算器

Dividing by half in ruby to create an effective calculator

过去我一直在研究计算器,但 运行 在需要除以一半时遇到了问题。我将在下面添加有问题的代码以及一个循环以使其保持打开状态。

on = true
while on == true do
    half = 1.0 / 2.0
    puts ("FUNCTION IN TESTING MODE, DO NOT EXPECT IT TO FUNCTION PROPERLY")
    puts ("Area of a triangle")
    print("What is the legnth of the base? ").to_i
    base = gets.chomp("base")
    print("\nWhat is the height? ")
    height = gets.chomp("height").to_i
    PreAreaT = base * height
    AreaT = PreAreaT * half
    puts("The area of the triangle is #{AreaT}")
end

所以基本上,我究竟如何让程序显示答案,而不是不输出任何答案?

编辑:事实证明上面的代码没有正确完成。我花了将近两周的时间问自己为什么它不起作用只是为了发现我有 .to_i 在打印语句而不是输入之后。

你的 to_i 呼叫在这里切换。

print("What is the legnth of the base? ").to_i
base = gets.chomp("base")

应该反过来。

print("What is the length of the base? ")
base = gets.chomp("base").to_i

此外,chomp 将尝试从字符串中删除任何出现的 baseheight。确保您的意图是删除这些事件;如果您想删除空格,则必须采用不同的方法。