2013-12-18 27 views
-1

我正在做一個筷子游戲,這是我正在做的第一個應用程序(真的是新的javascript),我想只要到達某一點,它就會重新啓動,直到敵人或手中有5把筷子。這裏是我的代碼:我想讓我的程序重新開始,有點像一個循環javascript

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 { 

} 
+1

「循環」確實是正確的詞。查找「javascript循環」,並跳過w3schools鏈接。 – sbking

+0

瞭解JavaScript。我們不是爲了爲您製作遊戲,只是幫助您瞭解您無法想象的代碼。 – PHPglue

回答

0

下面是做這件事:

var exit = false; 
while (exit != true) { 
    // Your code 

    // Use the next line to get out of the loop 
    // exit = true; 
} 

上述方法將它到達結束後退出循環。如果您想立即退出,可以使用break

+0

如果你需要完成循環,你會這樣做,但這取決於你在做什麼。 –

相關問題