Ruby 为什么我会得到一个“;没有将false隐式转换为字符串;
我是一个编程新手,作为一个有趣的小练习,我试着制作单词游戏jotto,但现在只有在你第一次回答正确的情况下它才有效。我想让程序告诉我,在我猜的单词中,有多少字母是正确答案,而不是字母是什么。我以为我有,但是 “if answer.include?word_array[iterations]==true”给了我一个错误,表示没有将alse隐式转换为字符串Ruby 为什么我会得到一个“;没有将false隐式转换为字符串;,ruby,Ruby,我是一个编程新手,作为一个有趣的小练习,我试着制作单词游戏jotto,但现在只有在你第一次回答正确的情况下它才有效。我想让程序告诉我,在我猜的单词中,有多少字母是正确答案,而不是字母是什么。我以为我有,但是 “if answer.include?word_array[iterations]==true”给了我一个错误,表示没有将alse隐式转换为字符串 def jotto() file_content = File.readlines("words.txt") i = 0
def jotto()
file_content = File.readlines("words.txt")
i = 0
new_array = []
while i < file_content.length
temp_str = file_content[i]
if temp_str.length == 4
new_array << temp_str
end
i = i + 1
end
answer = new_array[rand(new_array.length)]
puts(answer)
puts "Guess the secret word"
word = gets
word_array = []
word_array << word
i = 0
while answer != word
iterations = 0
w = 0
while iterations <= word.length
if answer.include? word_array[iterations] == true
w = w + 1
end
iterations = iterations + 1
end
puts("That's not correct but there are " + w + " of the same letters")
end
print("Yes! " + answer + " is the right answer!")
end
def jotto()
file\u content=file.readlines(“words.txt”)
i=0
新的_数组=[]
而i
if answer.include? word_array[iterations] == true
是redundent,也不是正确引用的
你尝试的是:
if answer.include?(word_array[iterations]) == true
但Ruby认为:
if answer.include? (word_array[iterations] == true)
但正确的方法是:
if answer.include? word_array[iterations]
无需检查它是否为真,因为include?
将为您提供true
或false
,并且已经可以应用于if
例如:
"abcd".include? 'a'
#=> true
"abcd".include? 'f'
#=> false
"abcd".include?('a') == true
#=> true
正如您所看到的,当您比较true==true
时,它仍然会给您一个true
,那么为什么还要费心比较它们呢
关于nil
转换错误,我认为这是因为:
while iterations <= word.length
迭代时语法错误
if answer.include? word_array[iterations] == true
是redundent,也不是正确引用的
你尝试的是:
if answer.include?(word_array[iterations]) == true
但Ruby认为:
if answer.include? (word_array[iterations] == true)
但正确的方法是:
if answer.include? word_array[iterations]
无需检查它是否为真,因为include?
将为您提供true
或false
,并且已经可以应用于if
例如:
"abcd".include? 'a'
#=> true
"abcd".include? 'f'
#=> false
"abcd".include?('a') == true
#=> true
正如您所看到的,当您比较true==true
时,它仍然会给您一个true
,那么为什么还要费心比较它们呢
关于nil
转换错误,我认为这是因为:
while iterations <= word.length
虽然迭代我认为Tiw指出了您的第一个问题,但有几个问题
下面是代码的更新版本,其中包含一些注释,解释了进行更改的原因
我还包括了一个更像ruby的版本
def jotto()
file_content = File.readlines("words.txt")
i = 0
new_array = []
while i < file_content.length
temp_str = file_content[i].chomp #added
if temp_str.length == 4
new_array << temp_str
end
i = i + 1
end
answer = new_array[rand(new_array.length)]
puts(answer)
puts "Guess the secret word"
# you need to get the word from the user inside the while loop.
# word = gets
# word_array = []
# word_array << word # This adds the word to an array of strings .. you want to turn the string into an array of characters
#i = 0 # not used
# added
word = nil
while answer != word
#added
word = gets.chomp
word_array = word.chars # see comment above
iterations = 0
w = 0
while iterations < word.length # was <=
if answer.include? word_array[iterations] # == true
w = w + 1
end
iterations = iterations + 1
end
puts("That's not correct but there are " + w.to_s + " of the same letters") if word != answer # there are better ways.
end
print("Yes! " + answer + " is the right answer!")
end
jotto()
我想Tiw指出了你的第一个问题,但有几个问题
下面是代码的更新版本,其中包含一些注释,解释了进行更改的原因
我还包括了一个更像ruby的版本
def jotto()
file_content = File.readlines("words.txt")
i = 0
new_array = []
while i < file_content.length
temp_str = file_content[i].chomp #added
if temp_str.length == 4
new_array << temp_str
end
i = i + 1
end
answer = new_array[rand(new_array.length)]
puts(answer)
puts "Guess the secret word"
# you need to get the word from the user inside the while loop.
# word = gets
# word_array = []
# word_array << word # This adds the word to an array of strings .. you want to turn the string into an array of characters
#i = 0 # not used
# added
word = nil
while answer != word
#added
word = gets.chomp
word_array = word.chars # see comment above
iterations = 0
w = 0
while iterations < word.length # was <=
if answer.include? word_array[iterations] # == true
w = w + 1
end
iterations = iterations + 1
end
puts("That's not correct but there are " + w.to_s + " of the same letters") if word != answer # there are better ways.
end
print("Yes! " + answer + " is the right answer!")
end
jotto()
欢迎来到stackoverflow。不要在你的标题中加入“(ruby)”之类的内容。这就是标签的作用。欢迎来到stackoverflow。不要在你的标题中加入“(ruby)”之类的内容。这就是标签的作用。非常感谢!我真的很挣扎,但我知道我错在哪里了,再次感谢你!非常感谢!我真的很挣扎,但我知道我错在哪里了,再次感谢你!