首页 > 解决方案 > 我是 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  

标签: rubyif-statement

解决方案


而不是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"或假。这两个陈述都必须是真实的才能起作用!


推荐阅读