let yourName;
do {
  yourName = prompt("Who are you?");
} while (!yourName);
console.log(yourName);
此代码在在线教科书中提供:https://eloquentjavascript.net/02_program_structure.html
我也试图以这种方式在控制台中运行它:
let funGame;
do {
    funGame = prompt("enter anything to get rid of this Game");
} while (!funGame);
console.log(funGame);
我正在使用Google Chrome浏览器来测试此代码。该代码仅运行一次,而应无限期运行,直到提供空字符串为止。

最佳答案

“Do While”将一直运行直到条件成立。在您的情况下,代码希望您必须编写一个名称以退出“do while”。

let yourName;
do {
  yourName = prompt("Who are you?");
} while (!yourName);
console.log(yourName);
如果您不希望出现这种情况,则可以删除“!”在“做一会儿”
let yourName;
    do {
      yourName = prompt("Who are you?");
    } while (yourName);
    console.log(yourName);
在这种情况下,您会遇到相反的情况。

08-15 18:05