首页 > 解决方案 > 如何让这个使用正确的 elsif 语句?

问题描述

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

我尝试过调整 .to_i 和 .to_f 以及其他各种东西。

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}"

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

标签: ruby

解决方案


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

您可以使用案例语句来确定适用的费率和折扣,但这是另一种方法。

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 美元,以此类推。单位成本是$0.90总重量为99磅或更多。(为什么这些数量在增加,我不能说。)

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

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

def ask(str)
  print str
  gets.to_f
end

price    = ask("Please enter the price of one book: ")  #=> 43.12
weight   = ask("Please enter the weight of one book: ") #=> 1.45
quantity = ask("Please enter the quantity of books: ")  #=> 30.0

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

subtotal = price * quantity
  #=> 1293.6

由于pricequantity是浮点数,subtotal因此也将是浮点数。(事实上​​,如果price是一个浮点数并且quantity是一个整数,subtotal那么它仍然是一个浮点数。)

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

def rate_or_discount(arr, amt)
  arr.find { |k,v| amt < k }.last
end

total_weight = quantity * weight
  #=> 43.5 
rate = rate_or_discount(RATES, total_weight)
  #=> 0.5
ship_cost = total_weight * rate
  #=> 21.75      

你明白为什么0.5被退回rate吗?

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

discount = 0.01 * rate_or_discount(DISCOUNTS, quantity)
  #=> 0.009000000000000001 
total_cost = (1 - discount) * subtotal + ship_cost
  #=> 1303.7076 

请注意,这(1 - discount)是一个浮点数,因为discount它是一个浮点数。

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

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

有关我使用的格式化代码的说明,请参阅Kernel#sprintf

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

printf("Subtotal: $%.2f\n", subtotal.round(2))
Subtotal: $1293.60

如果我改为写:

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

线

Subtotal: $1293.6

将被显示,其中尾随零被截断。

另请参见Enumerable#findFloat#round


推荐阅读