首页 > 解决方案 > 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

如何修改这个循环?

标签: ruby

解决方案


你可以这样做:

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

推荐阅读