加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – warning:条件字符串

发布时间:2020-12-16 20:28:28 所属栏目:百科 来源:网络整理
导读:使用下面的代码的第一位我收到两个警告消息: 警告:条件x2中的字符串字面值 if input == "N" || "n" #do thiselse input == "L" || "l" #do this 而不是使用这个结果没有警告 if input == "N" || input == "n" #do thiselse input == "L" || input == "l" #
使用下面的代码的第一位我收到两个警告消息:
警告:条件x2中的字符串字面值
if input == "N" || "n"
  #do this
else input == "L" || "l"
  #do this

而不是使用这个结果没有警告

if input == "N" || input == "n"
  #do this
else input == "L" || input == "l"
  #do this

我想知道为什么第一位代码会导致警告,并且使用它的缺点.

解决方法

更改输入==“N”|| “N”

input == "N" || input== "n"

你还必须使用else,而不是else

警告是说,而不是一个布尔值或测试,你有一个字符串字面值’n’,总是评估为真.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读