繁体   English   中英

Javascript / 如果,否则

[英]Javascript / if, else

需要我的代码帮助。 不明白为什么它不能正常工作。 描述:创建一个带有链接的外部脚本的网页,该脚本将用户年龄作为输入。 如果年龄小于 18 岁,网页应显示“您太年轻不能投票”,否则网页应继续询问此人是否注册投票或不使用提示。 如果否,页面应显示:“您需要先注册才能投票”如果是,程序显示:“您可以投票”使用变量的描述性名称、适当的主要和次要条件(if/else),并正确显示输出在基于条件的网页上。

 <head> <title> Voting! </title> </head> <body> <script type="text/javascript"> var age = prompt("Please enter your age"); if (age < 18) { document.write("You are too young to vote!"); } else if (age >= 18) { alert("You are eligible to vote!"); } var answare = prompt("Did you register to vote?"); if (answare == 'yes') { document.write("You can vote!"); } else { document.write("You need to register before you can vote"); } </script> </body> </html>

不是最佳解决方案,但可以让您了解为什么即使用户太年轻,它仍然会询问用户是否已注册。

 var age = prompt("Please enter your age"); if (age < 18) { document.write("You are too young to vote!"); } else if (age >= 18) { //wrap inside so this will only be called if the age is 18 or above alert("You are eligible to vote!"); var answare = prompt("Did you register to vote?"); if (answare == 'yes') { document.write("You can vote!"); } else { document.write("You need to register before you can vote"); } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM