Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 为什么回文检测器不工作(只返回一个输入)?_Ruby - Fatal编程技术网

Ruby 为什么回文检测器不工作(只返回一个输入)?

Ruby 为什么回文检测器不工作(只返回一个输入)?,ruby,Ruby,我正在练习Ruby,正在制作一个回文检测器 我已尝试将else更改为elsif,但均无效 print "Enter a Word and check if it's a Palindrome!" word = gets.chomp if word.reverse! == word print "The word you entered was a Palindrome!" else print "The word is not a Palindrome!" end 它只返回“您输入

我正在练习Ruby,正在制作一个回文检测器

我已尝试将
else
更改为
elsif
,但均无效

print "Enter a Word and check if it's a Palindrome!"
word = gets.chomp
if word.reverse! == word
  print "The word you entered was a Palindrome!"
else  
  print "The word is not a Palindrome!"
end

它只返回
“您输入的单词是回文!”
,但它应该返回其中一个。

您需要删除bang
因为在适当的位置反转字符串,并且条件始终为true

这就是正在发生的事情:

word = "whathever"
word.reverse!
#=> "revehtahw"

word
#=> "revehtahw"

"revehtahw" == "revehtahw"
#=> true
这就是您需要的:

word = "whathever"
word.reverse
#=> "revehtahw"

word
#=> "whathever"

"revehtahw" == "whathever"
#=> false

您需要删除bang
因为在适当的位置反转字符串,并且条件始终为true

这就是正在发生的事情:

word = "whathever"
word.reverse!
#=> "revehtahw"

word
#=> "revehtahw"

"revehtahw" == "revehtahw"
#=> true
这就是您需要的:

word = "whathever"
word.reverse
#=> "revehtahw"

word
#=> "whathever"

"revehtahw" == "whathever"
#=> false
,即
反向的对象消息已发送到。换句话说,
word.reverse
返回由
word
引用的对象,该对象始终等于自身,因此

word.reverse! == word
始终为

您要查找的是,它返回一个新字符串。

,即
反向的对象消息已发送到。换句话说,
word.reverse
返回由
word
引用的对象,该对象始终等于自身,因此

word.reverse! == word
始终为


您要查找的是,它返回一个新字符串。

正如其他人所说,您需要删除
运算符为
str.reverse就地操纵字符串

如果您是Ruby新手,还可以做一些很酷的事情,将代码放在If语句之前,以使其更具可读性并缩短它。此外,三元运算符
(?:)
也很有用

在If示例之前编码

print "Enter a Word and check if it's a Palindrome!"
word = gets.chomp

print "The word you entered was a Palindrome!" if word.reverse == word
print "The word is not a Palindrome!" if word.reverse != word
print "Enter a Word and check if it's a Palindrome!"
word = gets.chomp

puts word.reverse == word ? "It's a Palindrome!" : "It's not a Palindrome!"
三元示例

print "Enter a Word and check if it's a Palindrome!"
word = gets.chomp

print "The word you entered was a Palindrome!" if word.reverse == word
print "The word is not a Palindrome!" if word.reverse != word
print "Enter a Word and check if it's a Palindrome!"
word = gets.chomp

puts word.reverse == word ? "It's a Palindrome!" : "It's not a Palindrome!"

正如其他人所说,您需要删除
运算符为
str.reverse就地操纵字符串

如果您是Ruby新手,还可以做一些很酷的事情,将代码放在If语句之前,以使其更具可读性并缩短它。此外,三元运算符
(?:)
也很有用

在If示例之前编码

print "Enter a Word and check if it's a Palindrome!"
word = gets.chomp

print "The word you entered was a Palindrome!" if word.reverse == word
print "The word is not a Palindrome!" if word.reverse != word
print "Enter a Word and check if it's a Palindrome!"
word = gets.chomp

puts word.reverse == word ? "It's a Palindrome!" : "It's not a Palindrome!"
三元示例

print "Enter a Word and check if it's a Palindrome!"
word = gets.chomp

print "The word you entered was a Palindrome!" if word.reverse == word
print "The word is not a Palindrome!" if word.reverse != word
print "Enter a Word and check if it's a Palindrome!"
word = gets.chomp

puts word.reverse == word ? "It's a Palindrome!" : "It's not a Palindrome!"
当你在一个方法中添加一个“!”时(就像你在word.reverse中所做的那样!),ruby认为这意味着永久性地改变原来的单词,这样这个单词就会变成word.reverse。所以当你比较word.reverse时!对于单词,您已经将单词改为相反的单词,所以您输入的每个单词都是真实的

你需要改变的是

if word.reverse == word
这实际上会检查当前单词是否与反向单词相等。

当您在方法中添加“!”时(就像您使用word.reverse!),ruby将其视为永久更改原始单词,使单词成为word.reverse。所以当你比较word.reverse时!对于单词,您已经将单词改为相反的单词,所以您输入的每个单词都是真实的

你需要改变的是

if word.reverse == word

这将实际检查当前单词是否等于反向单词。

删除bang
反转
取下砰的一声
反转之后
事实是
反转是否到位并不是真正的相关位。事实上,它实际上与
相反的内容几乎无关可以。突出的一点是,它返回
self
,换句话说,它返回word
所指向的对象。@JörgWMittag,感谢您指出它以加深理解。这导致了
==
和自
反向之间的差异
返回
self
,在
=
equal?
之间没有任何区别。事实是
相反是否到位并不是真正的相关位。事实上,它实际上与
相反的内容几乎无关可以。突出的一点是,它返回
self
,换句话说,它返回word
所指向的对象。@JörgWMittag,感谢您指出它以加深理解。这导致了
==
和自
反向之间的差异
返回
self
,在
=
equal?
之间没有任何区别。有趣的是,这与大多数其他
不同String
中的code>methods–如果接收者恰好是回文,则此方法不会返回
nil
。有趣的是–与大多数其他
不同String
中的code>方法–如果接收者恰好是回文,则此方法不会返回
nil