这段代码有问题,但我无法弄清楚它是什么。页面无法正常工作或执行其应有的操作。我需要一个提示输入密码并将尝试限制为3的代码。在第三次尝试之后,它需要有一个警告框。我还没有添加警报框后面的内容。
<script>
var attempts = 3;
var answer = prompt("Password: ");
while (attempts != 0)
{
if (answer == "Psycho")
{
document.write("These are pictures of my kitten and her things.");
}
else
{
answer = prompt("Password: ");
attempts--;
}
}
if (attempts = 0)
{
alert("Incorrect Password");
}
</script>
答案 0 :(得分:0)
有几个选项可以修复您的代码。 一旦你完成了工作,你就可以回来。
<script>
var attempts = 3;
var answer = prompt("Password: ");
while (attempts != 0)
{
if (answer == "Psycho")
{
document.write("These are pictures of my kitten and her things.");
return;
}
else
{
answer = prompt("Password: ");
attempts--;
}
}
if (attempts == 0)
{
alert("Incorrect Password");
}
</script>
或者,如果你失败了,我会早点回来
<script>
var attempts = 4;
var answer = prompt("Password: ");
while (attempts > 0 && answer != "Psycho")
{
answer = prompt("Password: ");
attempts--;
}
if (attempts == 0)
{
alert("Incorrect Password");
}
else
{
document.write("These are pictures of my kitten and her things.");
}
</script>
答案 1 :(得分:0)
你有几个问题。您应该在用户输入提示后检查条目。或者不会检查最后一个条目。下一个问题是你没有退出循环。另一个问题是=
是如此分配的事实,如果你分配零,而不是检查它是否为零。
var attempts = 3;
while (attempts > 0) {
var answer = prompt("Password: ");
if (answer == "Psycho") {
document.write("These are pictures of my kitten and her things.");
break;
}
attempts--;
}
if (attempts == 0) {
alert("Incorrect Password");
}
&#13;