Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使其使用正确的elsif语句?_Ruby - Fatal编程技术网

Ruby 如何使其使用正确的elsif语句?

Ruby 如何使其使用正确的elsif语句?,ruby,Ruby,答案最终是应该的一半。它的使用率比它应该的要低,我不知道为什么。例如,重量为1.5的45本书将乘以比率=0.20,但它将乘以比率=0.50 我试着去适应各种各样的事情 puts "Please enter the price of one book." #Asks for book price price_input = gets #Stores book price puts "Please enter the weight of one book." #Asks for book weigh

答案最终是应该的一半。它的使用率比它应该的要低,我不知道为什么。例如,重量为1.5的45本书将乘以比率=0.20,但它将乘以比率=0.50

我试着去适应各种各样的事情

puts "Please enter the price of one book." #Asks for book price
price_input = gets #Stores book price
puts "Please enter the weight of one book." #Asks for book weight
weight_input = gets #Stores book weight
puts "Please enter the quantity of books." #Asks for number of books
quantity_input = gets #stores number of books
price = price_input.to_f
weight = weight_input.to_f
quantity = quantity_input.to_f

#Determine shipping cost per pound
total_weight = quantity * weight
total_weight = total_weight.to_f

if (total_weight < 2)
     rate = 0.10
   elsif ((total_weight >= 2) or (total_weight < 10))
     rate = 0.20
   elsif ((total_weight >= 10) or (total_weight < 40))
     rate = 0.30
   elsif ((total_weight >= 40) or (total_weight < 70))
     rate = 0.50
   elsif ((total_weight >= 70) or (total_weight < 100))
     rate = 0.75
   else (total_weight >= 100)
     rate = 0.90
end
rate = rate.to_f
ship_cost = total_weight * rate.to_f
ship_cost = ship_cost.to_f

#Determine discount based on quantity
if ((quantity >= 0) or (quantity <= 9))
  discount = 1
elsif ((quantity >= 10) or (quantity <= 39))
  discount = 0.9
elsif ((quantity >= 40) or (quantity <= 69))
  discount = 0.8
elsif ((quantity >= 70) or (quantity <= 99))
  discount = 0.7
else (quantity >= 100)
  discount = 0.6
end

discount = discount.to_f
d_cost = quantity * discount.to_f
d_cost = d_cost.to_f

#Display final costs
puts "Subtotal: #{price * quantity}"
puts "Shipping: #{ship_cost}"
puts "Discount: #{d_cost}"
puts "Total cost: #{(price * quantity - d_cost) + ship_cost}"
输入“请输入一本书的价格。”#询问书价
price_input=获取#存储书本价格
输入“请输入一本书的重量。”#询问书的重量
重量输入=获取#存储书本重量
输入“请输入图书数量。”#询问图书数量
数量_输入=获取#存储图书数量
价格=价格输入到
重量=重量输入到
数量=数量输入到
#确定每磅运费
总重量=数量*重量
总重量=总重量
如果(总重量<2)
比率=0.10
elsif((总重量>=2)或(总重量<10))
比率=0.20
elsif((总重量>=10)或(总重量<40))
比率=0.30
elsif((总重量>=40)或(总重量<70))
比率=0.50
elsif((总重量>=70)或(总重量<100))
比率=0.75
其他(总重量>=100)
比率=0.90
结束
速率=速率到
装运成本=总重量*费率
发货成本=发货成本
#根据数量确定折扣
如果((数量>=0)或(数量=10)或(数量=40)或(数量=70)或(数量=100)
折扣=0.6
结束
折扣=折扣
d_成本=数量*折扣
d_成本=d_成本到f
#显示最终成本
将“小计:{价格*数量}”
放入“装运:{ship#u cost}”
放入“折扣:{d#u成本}”
将“总成本:{(价格*数量-d#U成本)+装运成本}”

我得到的结果应该是它们的两倍。

我认为建议您如何重新组织代码可能是最有用的,但如果这能更好地回答您的具体问题。如果不是这样,我相信不久其他人会这样做

您可以使用案例陈述来确定适用的费率和折扣,但这里有一种替代方法

RATES     = [[2, 0.10], [10, 0.20], [40, 0.30], [70, 0.50], [100, 0.75],
             [Float::INFINITY, 0.90]]

DISCOUNTS = [[9, 1.0], [39, 0.9], [69, 0.8], [99, 0.7], [Float::INFINITY, 0.6]]
RATES
数组根据所有书籍的最大重量给出每本书的运输成本。如果重量小于
2
(比如磅),则成本为(比如美元)每本书0.10美元。如果总重量大于
2
但小于
10
,则单位运输成本为0.20美元,依此类推。如果总重量大于等于
99
磅,则单位成本为
$0.90
。(我无法说明为什么这些金额在增加。)

同样,如果订购的图书数量少于
9
,则每本书的折扣为
1%
;如果订购的图书数量至少少于
9
但低于
39
,则每本书的折扣为
0.9%
,依此类推。(我预计折扣百分比会上升,但我该说谁呢。)

现在让我们问用户一些问题。我们可以创建一个简单的方法来减少重复代码的数量

def ask(str)
  print str
  gets.to_f
end

右边的数量是我输入的三个值。接下来计算小计:

subtotal = price * quantity
  #=> 1293.6
由于
price
quantity
都是浮点数,
subtotal
也将是浮点数。(事实上,如果
price
是浮点数,
subtotal
仍然是浮点数。)

现在让我们计算运费和折扣。同样,为了避免重复代码,让我们创建一个简单的方法:

def rate_or_discount(arr, amt)
  arr.find { |k,v| amt < k }.last
end
您知道为什么
0.5
返回
rate

现在计算折扣,然后计算总成本

discount = 0.01 * rate_or_discount(DISCOUNTS, quantity)
  #=> 0.009000000000000001 
total_cost = (1 - discount) * subtotal + ship_cost
  #=> 1303.7076 
请注意,
(1-折扣)
是浮动的,因为
折扣
是浮动的

我们现在可以显示感兴趣的值

puts sprintf("Subtotal: $%.2f", subtotal.round(2))
Subtotal: $1293.60

puts sprintf("Shipping: $%.2f", ship_cost.round(2))
Shipping: $21.75

puts sprintf("Total cost: $%.2f", total_cost.round(2))
Total cost: $1303.71
有关我使用的格式代码的说明,请参见

可以改为写(注意换行符):

我是否写过:

puts "Subtotal: $#{subtotal.round(2)}"
线路

Subtotal: $1293.6
将被显示,其尾部零点已关闭


另请参见和。

若要设置代码块,请缩进4个或更多空格,或选择未缩进的代码,然后单击编辑框上方的
{}
。若要设置文本中的代码,请在其周围加上背景标记(例如,`[1,2,3]`)。(若要显示这些背景标记,我必须将其删除。若我没有这样做,您将看到
[1,2,3]
)由于这个代码是不完整的或非功能性的,例如:“代码>价格=PRICESSIONT。ToFF但<代码> Price输入< /代码>从未被定义。如果您想从输入<代码>获得一个浮动。toAFF 是个诀窍。当编写这样的代码时,请考虑写方法,而不是只在中间敲击代码。ight rate calculator应该包含类似于
def rate\u for_weight(total_weight)
的内容,然后您可以简化其实现方式。我已经对其进行了正确编辑,并包含了我编写的其他内容(尽管,如果现在太多,我很抱歉)。使用诸如byebug之类的调试器gem来逐步检查代码会给您带来很多好处。但是,至少您可以在检查值的过程中放置一组
put
语句
printf("Subtotal: $%.2f\n", subtotal.round(2))
Subtotal: $1293.60
puts "Subtotal: $#{subtotal.round(2)}"
Subtotal: $1293.6