问题:当年龄无效时如何连续提示用户(低于0,高于200,或不是数字)?
var userAge = prompt("Please enter your age");
while (isNaN(userAge)){
alert("That's not a number, please retry.");
var userAge = prompt("Please re-enter your age.");
}
if (userAge == 0){
alert("So you're a baby!");
}
while( userAge < 0 || userAge >= 200){
alert("I think you may be lying about your age.");
var userAge = prompt("Please re-enter your age, don't lie this time.");
{continue}
}
if (userAge > 0 || userAge < 200){
alert("That's a good age.");
}
</script>
答案 0 :(得分:1)
您可以使用break
语句从while循环返回。例如:
var userAge = prompt("Please enter your age");
while (true) {
if (isNaN(userAge)) {
alert("That's not a number, please retry.");
} else if (userAge < 0 || userAge >= 200) {
alert("I think you may be lying about your age.");
} else {
break;
}
var userAge = prompt("Please re-enter your age.");
}
if (userAge == 0) {
alert("So you're a baby!");
} else {
alert("That's a good age.");
}