【问题标题】:How do you change a do while loop to a while loop?如何将 do while 循环更改为 while 循环?
【发布时间】:2020-08-28 22:32:08
【问题描述】:

我的作业中的一个问题是将 do while 切换到 while 循环。我有点卡住了,我尝试以不同的方式移动它,但我的代码仍然无法正确输出。

do {
  userChoice = window.prompt("Press a to add a Robot Step to take\n, Press r to remove a Robot Step last Step to take\n, Press f to remove a Robot First Step to take\n, Press p to make the robot move the steps,");
  userChoice = userChoice.toLowerCase();

  switch(userChoice) {
    case "a":
      addStepsPerMovement(stepsPerMovement);
      break;
    case "r":
      removeLastMovement(stepsPerMovement);
      break;
    case userChoice == "f":
      removeFirstMovement(stepsPerMovement);
      break;
    case "p":
      printByWhileLoop (stepsPerMovement);
      break;
    default:
      document.write("Erroneous Choice\n");
      break;
  }
  userContinue = window.prompt("Do you want to continue?, y or n");
  userContinue = userContinue.toLowerCase();

} while (userContinue == "y") 

【问题讨论】:

  • 嗨 Leny,你能详细说明你真正想要实现的目标吗?
  • 您好,您能否提供一些您的试验,以便我们了解您的转换有什么问题?

标签: javascript while-loop switch-statement do-while


【解决方案1】:

您可以轻松地将 do-while 替换为:

 while(true) {
   /* body */
  if(!/*condition*/)
     break;
 }

【讨论】:

  • 我怀疑这是否能满足任务......另外,我个人不喜欢 while(true) 循环。
【解决方案2】:

那个?

let userContinue = "y"

while (userContinue == "y") 
  {
  userChoice = window.prompt("Press a to add a Robot Step to take\n, Press r to remove a Robot Step last Step to take\n, Press f to remove a Robot First Step to take\n, Press p to make the robot move the steps,");
  userChoice = userChoice.toLowerCase();

  switch(userChoice)
    {
    case "a":
      addStepsPerMovement(stepsPerMovement);
      break;
    case "r":
      removeLastMovement(stepsPerMovement);
      break;
    case "f":
      removeFirstMovement(stepsPerMovement);
      break;
    case "p":
      printByWhileLoop (stepsPerMovement);
      break;
    default:
      document.write("Erroneous Choice\n");
      break;
    }
  userContinue = window.prompt("Do you want to continue?, y or n");
  userContinue = userContinue.toLowerCase();
  } 

【讨论】:

  • 不,很遗憾,我试过了,但没用。
  • @LennyT。更正了! (我没有看到你的错误 => case userChoice == "f"
  • 等待这是正确的我需要更改顶级用户继续“y”谢谢
  • @LennyT。你会验证我的回答吗?
【解决方案3】:

嗨,这很简单, do while 和 while 的区别是: 在 do while 中,代码块将执行一次,而与 while 条件无关。 但是在 while 中,当且仅当条件为真时才执行。

所以根据你的代码,这里是sn-p:

声明一个包含 do while 的主体部分的函数。

function dowhiletoWhile() {
   userChoice = window.prompt("Press a to add a Robot Step to take\n, Press r to remove a Robot Step last Step to take\n, Press f to remove a Robot First Step to take\n, Press p to make the robot move the steps,");
  userChoice = userChoice.toLowerCase();

  switch(userChoi`enter code here`ce) {
    case "a":
      addStepsPerMovement(stepsPerMovement);
      break;
    case "r":
      removeLastMovement(stepsPerMovement);
      break;
    case userChoice == "f":
      removeFirstMovement(stepsPerMovement);
      break;
    case "p":
      printByWhileLoop (stepsPerMovement);
      break;
    default:
      document.write("Erroneous Choice\n");
      break;
  }
  userContinue = window.prompt("Do you want to continue?, y or n");
  userContinue = userContinue.toLowerCase();
}



dowhiletoWhile();


 while (userContinue == "y"){
 dowhiletoWhile();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2021-05-22
    • 2012-01-18
    • 2021-07-27
    • 1970-01-01
    相关资源
    最近更新 更多