gpt4 book ai didi

javascript - 循环提示直到用户决定终止程序

转载 作者:行者123 更新时间:2023-12-03 02:42:02 25 4
gpt4 key购买 nike

这是我的第一篇文章。我正在尝试构建一个基本的 js 程序,该程序将提示用户“输入英雄以查看他们是否在数组中”。程序按预期执行,除了我希望在第一个“do{”之后出现提示,继续提示用户,直到用户输入“Q”(是的,我对此很陌生)。相反,我的程序在找到英雄后停止提示用户。谢谢,莎拉。

<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
</script>
</head>
<body>
<script type="text/javascript">

//Program name: Review
//Purpose: Allow user to search through array for hero
// Author: Sarah
//DLM: 15 Jan 2017
var more; //continue looping?
var hero; // hero to search for in the array
var ES = ""; //Space
var BR = "<br/>";
var ARRAYSIZE = 4;
var found=false;

//array of superheroes
var superheroes = new Array("batman", "superman", "spiderman", "hulk");

//continue until user wants to quit
do{
hero = prompt("Enter a hero to see if they are in array.", ES);

//to lower case
hero.toLowerCase = hero;

// loop through heroes, checks if user entered hero is contained within
for (index=0; index < 4; index++){
if (superheroes[index].toLowerCase() == hero){
found=true;
break;
}
}
if(found){
document.write(superheroes[index] + " is in the array." + BR)
quit="Q";
}else{
quit = prompt("Hero not found. Do you want to continue? (Y for yes, Q to quit)", ES);
}
}while (quit.toUpperCase() != "Q");
</script>
</body>
</html>

最佳答案

所以如果我理解正确的话,你希望程序不断提示,即使英雄被发现了?

在这种情况下,我建议删除这一行:quit="Q";

关于javascript - 循环提示直到用户决定终止程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48289114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com