ruby - 案例陈述 - Ruby
问题描述
我正在编写一个简单的方法来计算我需要支付多少租车费用。但是,当我执行它时,它总是返回 else 语句 "nothing" 。这里有什么问题?
def rental_car_cost(d)
case d
when d < 3
puts d * 40
when d >=3 && d < 7
puts d * 40 - 20
when d >= 7
puts d * 40 - 50
else
puts "nothing"
end
end
rental_car_cost(5)
nothing
解决方案
case d
期望 中的单个值when
,而不是条件。
def rental_car_cost(d)
case
when d < 3
puts d * 40
when d >=3 && d < 7
puts d * 40 - 20
when d >= 7
puts d * 40 - 50
else
puts "nothing"
end
end
如果你想使用,case d
那么你应该有这样的东西:
def rental_car_cost(d)
case d
when 3
puts d * 40
when 7
puts d * 40 - 20
else
puts "nothing"
end
end
查看此SO 帖子以获取更多示例。
推荐阅读
- laravel - 如何在 vs-upload 上使用自定义操作?
- node.js - 错误:监听 EADDRINUSE:地址已在使用 :::3000 试图在节点 js 中运行服务器
- html - 所需的媒体查询帮助和解释
- laravel - 如何在前端页面上使用 Laravel Grid View 和十月 CMS?
- flutter - 颤动:屏幕锁定时识别方向
- django - 我是 Django 新手,谁能帮我完成以下任务....?
- python - 如何在 Django 中序列化多对多字段数据?
- c - C中的信号处理
- php - 通过 PHP 重复 Google Slides API“请求”的函数
- github-actions - 提取 .zip 文件的 Github 操作步骤?