ruby - Ruby循环放置两条不同的消息
问题描述
我有一些大脑日食,假设我有一个方法:
def conditionals(x)
case
when x > 2
puts "x is greater than 2"
when x == 3
puts "x is 3"
else
puts "I can't guess the number"
end
end
如果x = 3
我想打印两条消息:
x is greater than 2
x is 3
现在它只会打印我一个:
x is greater than 2
如何修改这个循环?
解决方案
你可以这样做:
def conditionals(x)
puts "x is greater than 2" if x > 2
puts "x is 3" if x == 3
puts "I can't guess the number" if x <= 2
end
推荐阅读
- r - 创建表示计数的新向量
- javascript - 为什么每次刷新logo我的页面都会自动上去?
- laravel - Laravel 在特定文件夹中创建迁移文件
- c++ - 使用零计数调用“memcpy”会很昂贵吗?
- ruby-on-rails - 一对多关系:找不到“id”=3 的 Ofertum
- spring - Spring JPA 如何使用两个不同的数据库连接进行读写操作?
- if-statement - 如何更改此脚本以添加另一个条件
- python - 在odoo中使用相同字段添加多个条目
- python - 将 DataFrame 转换为系列
- python - 如何为整个django项目实现一个共享变量?