How to check if a number is between two other numbers in Actionscript 3?

人盡茶涼 提交于 2019-12-25 17:05:38

问题


How can i check if a number is between two other numbers like:

pseudocode:

var = 458;

if (var is between 0 and 1000) give positive.

if (var is between 1001 and 2000) give negative.

if (var is between 2001 and 3000) give negative.

in AS3?

Thanks in advance.


回答1:


If You will check it many times, simply create function :

function check(min:Number , value:Number , max:Number):Boolean{
    return min > value ? false : ( max < value ? false : true );
}

It will return true if value is between min and max.




回答2:


if (var >= 0 && var <= 1000) {
  return true
}
else if (var >= 1001 && var <= 2000) {
  return false
}
else if (var >= 2001 && var <= 3000) {
  return false
}

But conditions 2 and 3 both return false, and the condition also evaluate to true/false, so you could simply:

return (var >= 0 && var <= 1000)



回答3:


There's a method in the framework just for that:

mx.utils.ObjectUtil::numericComapre()

From the docs:

Compares two numeric values. Returns int — 0 is both numbers are NaN. 1 if only a is a NaN. -1 if only b is a NaN. -1 if a is less than b. 1 if a is greater than b.



来源:https://stackoverflow.com/questions/7864858/how-to-check-if-a-number-is-between-two-other-numbers-in-actionscript-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!