我想让我的程序重新运行开头,有点像循环 JavaScript

I'd like to make my program re-run the beginning, kind of like a loop javascript

本文关键字:循环 JavaScript 开头 我的 程序 重新运行      更新时间:2023-09-26

我正在制作一个筷子游戏,这是我正在制作的第一个应用程序(对javascript来说真的很新),我希望它在到达某个点时重新启动,直到敌人或你的手都有5根筷子。 这是我的代码:

var nocLeft = 1;
var nocRight = 1;
var handChoice = prompt("You now have " + nocLeft + " chopstick(s) on your left hand, and " + nocRight+ " chopstick(s) on your right hand, which hand do you want to use?").toLowerCase();
if (handChoice === right){
var ehc = prompt("Which hand are you attacking? The enemy's left hand has " + noceLeft + ", and their right hand has " + noceRight + " chopsticks.").toLowerCase();
switch(ehc) {
    case 'right': 
        console.log("You chose the right hand!");
    break;
    case 'left':
        console.log("You chose the right hand!");
    break;
    default:
    console.log("please answer with 'left' or 'right', thanks!");
    }
}
else if (handChoice === left){
var ehc = prompt("Which hand are you attacking? The enemy's left hand has " + noceLeft + ", and their right hand has " + noceRight + " chopsticks.").toLowerCase;
}
else {
} 

这里有一种方法可以做到这一点:

var exit = false;
while (exit != true) {
  // Your code
  // Use the next line to get out of the loop
  // exit = true;
}

上述方法在到达末尾后将退出循环。 如果您想立即退出,可以使用 break .