ruby - 我是 ruby 语言的新手,正在尝试创建一个文本冒险游戏,我想包含两个字符串
问题描述
我希望用户输入任何内容,但它应该包含两个字符串,如“look”和“right”,如果这两个字符串都包含在句子中,那么程序将转到下一行。我是 ruby 的新手,只是让自己熟悉这种语言。
我已经尝试过&&
,||
但它不起作用
puts " in which direction do you want to look"
input = gets.chomp.to_s
if input.include? == "look" && "right"
puts " There is a dead end on the right"
elsif input.include? "look" && "left"
puts "there is a narrow way , going towards the woods"
else
puts "i dont understand what you are trying to say"
end
解决方案
而不是input.include? "look" && "right"
,您需要一一比较语句。
if input.include?("look") && input.include?("right")
puts " There is a dead end on the right"
elsif input.include?("look") && input.include?("left")
puts "there is a narrow way , going towards the woods"
else
puts "i dont understand what you are trying to say"
end
从逻辑上讲,input.include? "look"
要么返回真或假,要么返回真input.include? "right"
或假。这两个陈述都必须是真实的才能起作用!
推荐阅读
- windows - 在 Windows 10 设备驱动程序中使用互斥锁导致代码 39
- angular - 使用公共模块文件夹的多个 Angular 应用程序
- powerbi - 如何在 Power BI 中合并多个表,同时附加一个包含表名称的新列?
- java - 当lib类获取键值时,如何修改属性文件中的值?
- javascript - 纹理是将音频数据传输到 GLSL 的唯一方法吗?
- erlang - 为什么我在运行 eunit 测试时会出错?
- ruby-on-rails - Rails 4:删除目录后如何重新创建 Postgres 数据库
- ubuntu - 如何让 orca 在 ubuntu 上的 Firefox 中阅读网页
- c++ - 为什么可以在析构函数中删除未初始化的指针,但不能在 main 中删除?
- powershell - 保持变量私有,但仍然在函数中使用它