【问题标题】:Looping a prompt until user decides to terminate program循环提示直到用户决定终止程序
【发布时间】:2018-01-16 20:14:31
【问题描述】:

这是我的第一篇文章。我正在尝试构建一个基本的 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>

【问题讨论】:

  • 因为你在找到它的时候将它设置为Q.....所以如果你不想让它停止,那么你应该删除那条线......
  • 顺便说一句,hero.toLowerCase() = hero; 需要 () 来调用该方法。
  • 不应该也是hero = hero.toLowerCase();吗?

标签: javascript html arrays loops do-loops


【解决方案1】:

所以如果我理解正确,你希望程序继续提示,即使找到了英雄?

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

【讨论】:

    【解决方案2】:

    你退出的原因是:

    quit="Q";
    

    上一行也少了一个分号。

    【讨论】:

      【解决方案3】:

      您的程序因错误而停止。检查您的控制台。

      hero.toLowerCase = hero; 倒退了。应该是hero=hero.toLowerCase(); 此外,您可以使用indexOfincludes 来查看英雄是否在数组中,而不是循环。

      如果您希望程序在找到英雄时​​询问“Q”,请删除 else 并将退出提示放在下面,这样每次都会询问。

      //Program name: Review
      //Purpose: Allow user to search through array for hero
      // Author: Sarah
      //DLM: 15 Jan 2017
      var hero; // hero to search for in the array
      var ES = ""; //Space
      var BR = "<br/>";
      var found = false;
      var quit;
      
      //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 = hero.toLowerCase();
      
        // checks if user entered hero is contained within
        found = superheroes.indexOf(hero) > -1;
        
        if (found) {
          document.write(hero + " is in the array." + BR);
        }
      
        quit = prompt("Do you want to continue? (Y for yes, Q to quit)", ES);
      
      } while (quit.toUpperCase() != "Q");

      【讨论】:

      • OP 还希望“一直提示用户,直到用户输入'Q'”看起来你的示例在找到英雄后停止。
      【解决方案4】:

      在英雄提示行之后,在 do 循环内。

      if (hero.charAt(0) == 'Q') break;
      

      这将检查英雄的第一个字符是否为大写 Q。 如果是,它会跳出 do while 循环,然后你就完成了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        • 2018-08-22
        • 2022-01-23
        • 1970-01-01
        • 1970-01-01
        • 2016-05-04
        • 1970-01-01
        相关资源
        最近更新 更多