result =
if gets == "123" then true
else false
end
puts result
输入123后
以上代码输出false。为什么
如何才能使键盘输入的字符串和程序中的字符串相等
使得进行比较后得到true
result =
if "123" == "123" then true
else false
end
puts result
而这段代码就可以输出true。
本文通过两个示例探讨了在程序中如何正确地比较字符串。第一个示例中,使用键盘输入与预设字符串进行比较时返回了false,而第二个直接比较相同预设字符串则返回true。文章分析了这种现象的原因,并提供了确保比较结果为true的方法。
result =
if gets == "123" then true
else false
end
puts result
result =
if "123" == "123" then true
else false
end
puts result

被折叠的 条评论
为什么被折叠?