如何将do while循环更改为while循环?

问题描述 投票:0回答:2

关于我的作业的问题之一是将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") 
javascript while-loop switch-statement do-while
2个回答
0
投票

您可以轻松地与以下操作互换执行时间:

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

0
投票

那个?

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();
  } 
© www.soinside.com 2019 - 2024. All rights reserved.