在 ruby 的 case 语句中使用“and”关键字的正确方法是什么?这是一个例子:
编写一个程序,询问用户年龄并根据输入打印以下字符串:
0 to 2 => "baby"
3 to 6 => "little child"
7 to 12 => "child"
13 to 18 => "youth"
18+ => "adult"
示例 1
INPUT
Enter the age:
3
OUTPUT
little child*
puts "Enter the age:"
age = gets.chomp.to_i
#Write your code here
case (age)
when age >= 0 and <= 2 then puts("baby")
when age > 2 and < 7 then puts("little child")
when age > 6 and < 13 then puts("child")
when age > 12 and < 18 then puts("youth")
when age > 18 then puts("adult")
end
#