Jus*_*tin 3 ruby ruby-on-rails
这可能是一个愚蠢的问题,但我无法让它发挥作用.很确定我错过了什么.
我想设置一个布尔值,false
然后true仅在满足条件时将其设置为.
boolTest = false
until boolTest = true
puts "Enter one fo these choices: add / update / display / delete?"
choice = gets.chomp.downcase
if choice == "add" || choice == "update" || choice == "display" || choice == "delete"
boolTest = true
end
end
Run Code Online (Sandbox Code Playgroud)
只是刚刚开始学习Ruby,所以也许我会混淆其他语言的功能.
既然你正在使用until,那就是有效地写出来了while not boolTest.你不能使用=,因为那是为作业保留的; 相反,省略布尔条件.检查布尔值对布尔值没有价值; 如果你真的想保留它,你必须使用==.
boolTest = false
until boolTest
puts "Enter one fo these choices: add / update / display / delete?"
choice = gets.chomp.downcase
if choice == "add" || choice == "update" || choice == "display" || choice == "delete"
boolTest = true
end
end
Run Code Online (Sandbox Code Playgroud)
作为优化/可读性提示,您还可以调整布尔条件,以便没有重复的语句choice; 你可以声明一个数组中的所有thoe字符串,并检查是否choice存在于数组中include?.
boolTest = false
until boolTest
puts "Enter one fo these choices: add / update / display / delete?"
choice = gets.chomp.downcase
boolTest = %w(add update display delete).include? choice
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14053 次 |
| 最近记录: |