如何限制此代码中的年龄? [英] How to restrict an age in this code ?

查看:69
本文介绍了如何限制此代码中的年龄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var x =提示(在这里输入年龄,10);

if(x> 60)

{

document.write(old);

}

else if(x> 40)

{

document.write('Medium age');

}

else if(x> 18)

{

document.write('young');

}

else if(x> 0)

{

document.write('child');

}

else {

alert(无效输入);

}



我的尝试:



如果我输入的数字超过60甚至任何数字,如200,300,2000,2000000,得到的回答是更老。任何人都可以告诉我如何验证它我的意思是年龄不应超过120年。

var x=prompt("enter age here", "10");
if(x>60)
{
document.write("older");
}
else if(x>40)
{
document.write('Medium age');
}
else if(x>18)
{
document.write('young');
}
else if (x>0)
{
document.write('child');
}
else {
alert("invalid input");
}

What I have tried:

if i am entering a number more than 60 even any number like 200,300,2000,2000000, getting answer is 'older'. Can anybody tell me how to validate it i mean age should not be more than 120 years.

推荐答案

只需添加另一个if



Just add another "if"

if (x>120)
{
document.write("too old");
}
else if(x>60)
{
document.write("older");
}


这是因为你的代码就是这样编写的。当x大于60时,你已经要求你的程序说老了。



尝试下面这样的事情 -

It's because your code is written that way. You have asked your program to say older when x is greater than 60.

Try something like following-
var x=prompt("enter age here", "10");
if(x>60 && x<=120)
{
document.write("older");
}
//...rest of the code as it is.





希望,有帮助:)



Hope, it helps :)


这篇关于如何限制此代码中的年龄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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