【发布时间】:2020-07-24 16:00:28
【问题描述】:
我想在用户写 a 或 b 之前回忆这个案例。我不想使用“案例” 特别。
我只想从用户那里得到输入,而不是得到其他东西。如果他写别的东西,他应该一直写到他写出a或b为止。
str = gets.chomp.to_s
case str
when "a"
print "nice a"
when "b"
puts "nice b"
else
puts "please do it again"
end
class person
attr_accessor :name , :surname #and other attributes
end
#There will be a method here and it will run when the program is opened.
#The method will create the first object as soon as the program is opened.
#The new object that the user will enter will actually be the 2nd object.
puts "What do you want to do?
add
list
out"
process = gets.chomp.to_s
case process
when "add"
#in here user will add new objects of my class
when "list"
#in here user will show my objects
when "out"
puts "Have a nice day"
else
puts "please do it again"
end
事实上,如果你看它,很多动作都会因为用户输入正确的输入而被执行。我想讲的在这个例子中更详细。根据用户的输入,会有调用方法、添加对象等动作。
我在计算机上编写了大部分代码。但我仍然无法解决我的第一个问题。
【问题讨论】:
标签: ruby ruby-on-rails-3 switch-statement conditional-statements case