无限循环?有什么建议么?

输入名称时我无法继续执行程序,我必须输入“Q”。谁能告诉我我做错了什么?


empName = prompt("Enter employee's name or Q to quit: ");


    while (empName != "Q") {

      empTot = 0;

    

      for (var i = 0; /  i < 5;  i++) {

        prompt("Enter the amount of day " + (i + 1) + ": ");

    

    }

    document.write(empName + ": total is " + empTot);

墨色风雨
浏览 126回答 3
3回答

一只萌萌小番薯

你里面有一个“/”。尝试for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;5;&nbsp;i++)

繁星点点滴滴

你的 while 循环缺少 }

料青山看我应如是

您应该避免使用 while 循环,除非您知道自己在做什么,因为可能会创建无限循环。相反,使用 if 语句来检查输入是否为“Q”。为了可用性,我提供了退出不区分大小写的可能性。你的 for 循环中有一个拼写错误,因为你在/其中,并且你从未将 empTot 与提示中的答案一起添加。我将提示封装在 parseInt 中,并通过 || 0在末尾添加来添加默认值。您也从未使用 声明过新变量var。声明变量时应该始终这样做,否则某些浏览器将无法理解变量的来源。var empName = prompt("Enter employee's name or Q to quit: ");if (empName && empName.toLowerCase() != "q") {&nbsp; var empTot = 0;&nbsp; for (var i = 0; i < 5; i++) {&nbsp; &nbsp; empTot += parseInt(prompt("Enter the amount of day " + (i + 1) + ": ")) || 0;&nbsp; }&nbsp;&nbsp;&nbsp; document.write(empName + ": total is " + empTot);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript