begin
selected_option = gets.chomp
if selected_option == 1
puts "Welcome to the Welcome Screen!"
elsif selected_option == 2
puts "This is the options menu."
elsif selected_option == 3
puts "Logging out. Goodbye!"
else
puts "Please select a valid option."
end
end while not selected_option == 3
我输入1或2或3,我总是得到"请输入有效选项"信息。我猜这是因为chomp方法将输入检索为字符串。
围绕这些sans使用选项周围的引号?
答案 0 :(得分:3)
gets.chomp.to_i
将其转换为整数。
您可能还想使用开关:
begin
selected_option = gets.chomp.to_i
case selected_option
when 1
puts "Welcome to the Welcome Screen!"
when 2
puts "This is the options menu."
when 3
puts "Logging out. Goodbye!"
else
puts "Please select a valid option."
end
end while not selected_option == 3