将变量与一系列值进行比较 [英] Comparing a variable to a range of values

查看:68
本文介绍了将变量与一系列值进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数学中,符号 18<年龄< 30 表示年龄必须介于18和30之间。是否可以在if语句中使用这种表示法?例如,我尝试执行

In mathematics, the notation 18 < age < 30 denotes that age must lie between the values 18 and 30. Is it possible to use this kind of notation in the if statement? For example, I've tried executing

if(18 < age < 30)

,我得到的输出很奇怪,所以不太正确。有没有办法做到这一点,所以我只需要写

and I get weird output, so it's not quite right. Is there a way to do this or so I simply have to write

if(age > 18) /*blah*/;
else if(age < 30) /*same blah*/;


推荐答案

您可以执行以下操作:

if (18 < age && age < 30) /*blah*/;

这篇关于将变量与一系列值进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆