javascript 限制javascript变量上的最小/最大整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12424185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Limit min/max integer on javascript variable
提问by zhaobaloth
Im trying to make a talent calculator of sorts, clicking the 'skill' will decrease this variable and right-clicking will increase the variable.
我正在尝试制作各种天赋计算器,单击“技能”将减少此变量,右键单击将增加变量。
The problem i have is i want the variable to be limited from going over 50 or below 0.
我遇到的问题是我希望将变量限制在 50 以上或低于 0。
Now ive googled and searched here for variable limits and max integers but nothing gives me an idea how to do this, i think im wording my searches wrong.
现在我用谷歌搜索并在这里搜索变量限制和最大整数,但没有任何东西让我知道如何做到这一点,我认为我的搜索措辞是错误的。
Could someone give me an idea how to do this or point me in the right direction please, thanks.
有人可以给我一个想法如何做到这一点或请指出我正确的方向,谢谢。
Variable im using:
变量 im 使用:
var a=50;
function decrease(){a--;document.getElementById('boldstuff').innerHTML= +a;}
function increase(){a++;document.getElementById('boldstuff').innerHTML= +a;}
采纳答案by Leri
In function decrease
after a--
add
添加decrease
后在函数中a--
if (a < 0)
a = 0;
And in increase
:
并在increase
:
if (a > 50)
a = 50;
Or use Math.min(value, minValue)
and Math.max(value, maxValue)
.
或使用Math.min(value, minValue)
和Math.max(value, maxValue)
。
回答by Vincent Thibault
You can use Math.min and Math.max.
您可以使用 Math.min 和 Math.max。
var a=50;
function decrease(){ a = Math.max( a-1,0) ; document.getElementById('boldstuff').innerHTML= a;}
function increase(){ a = Math.min( a+1,50); document.getElementById('boldstuff').innerHTML= a;}
回答by Mohit Pandey
Try this:
试试这个:
var a=50;
function decrease(){
if(a > 0){
a--;
document.getElementById('boldstuff').innerHTML= +a;
}
}
function increase(){
if(a < 50){
a++;
document.getElementById('boldstuff').innerHTML= +a;
}
}
回答by vivek
Try this:
试试这个:
var a=50;
function decrease(){if(a>0){a--;document.getElementById('boldstuff').innerHTML= +a;}}
function increase(){if(a<50){a++;document.getElementById('boldstuff').innerHTML= +a;}}
回答by Guffa
Just check the value before you change it, so that you only change it if it hasn't reached the limit:
只需在更改之前检查该值,以便仅在未达到限制时才更改它:
var a=50;
function decrease(){
if (a > 0) {
a--;
document.getElementById('boldstuff').innerHTML = a;
}
}
function increase(){
if (a < 50) {
a++;
document.getElementById('boldstuff').innerHTML = a;
}
}
(The code above assumes integer values. If you assigned 49.9
to the variable and called increase
, it would increase the variable to 50.9
. If you needed to guard against floating point numbers also, you would do the checks like if (a - 1 >= 0)
and if (a + 1 <= 50)
.)
(上面的代码假定整数值。如果您分配49.9
给变量并调用increase
,它会将变量增加到50.9
。如果您还需要防范浮点数,您可以执行类似if (a - 1 >= 0)
和的检查if (a + 1 <= 50)
。)
回答by Christopher Ferrin
//// your max ///// your min if(type > 4){type = 4;} if(type < 0){type = 0;}
//// 你的最大值 ///// 你的最小值 if(type > 4){type = 4;} if(type < 0){type = 0;}
simply if its outside either bound set it to that bound
只是如果它在任一边界之外将其设置为该边界