2014-06-29 23 views
-2

我想连续循环运行下面的程序直到我的数组长度。我希望程序像第一次输入那样运行,然后在第一次比较之后程序不应该结束,而是应该运行,直到满足for循环条件。该程序如下:如何在JavaScript中使用循环,特别是在我的程序中?

var userChoice = prompt("What would you like to select: rock, paper or scissors?"); 
var computerChoice = Math.random(); 

    if(computerChoice<0.34) { 
     computerChoice = "rock"; 
    } 
    else if(computerChoice<=0.67) { 
     computerChoice = "paper"; 
    } 
    else if(computerChoice<=1) { 
     computerChoice = "scissors"; 
    } 
    console.log("Computer: "+computerChoice); 
var compare = function(choice1, choice2) { 
    if(choice1 === choice2) { 
     return "It is a tie!"; 
    } 
    else if(choice1 === rock) { 
     if(choice2 === paper) { 
      return "paper wins"; 
     } 
     else { 
      return "rock wins"; 
     } 
    } 
    else if(choice1 === paper) { 
     if(choice2 === rock) { 
      return "paper wins"; 
     } 
     else { 
      return "scissors wins"; 
     } 
    } 
    else if(choice1 === scissors) { 
     if(choice2 === paper) { 
      return "scissors wins"; 
     } 
     else { 
      return "rock wins"; 
     } 
    } 
}; 
compare(userChoice, computerChoice); 

我的数组是:

我想它是:

var userChoice = ["rock", "paper", "scissors"]; 
for(i=0; i<userChoice.length; i++) { 
    var userChoice = prompt("What would you like to select: rock, paper or scissors?"); 
var computerChoice = Math.random(); 
    if(computerChoice<0.34) { 
     computerChoice = "rock"; 
    } 
    else if(computerChoice<=0.67) { 
     computerChoice = "paper"; 
    } 
    else if(computerChoice<=1) { 
     computerChoice = "scissors"; 
    } 
    console.log("Computer: "+computerChoice); 
var compare = function(choice1, choice2) { 
    if(choice1 === choice2) { 
     return "It is a tie!"; 
    } 
    else if(choice1 === rock) { 
     if(choice2 === paper) { 
      return "paper wins"; 
     } 
     else { 
      return "rock wins"; 
     } 
    } 
    else if(choice1 === paper) { 
     if(choice2 === rock) { 
      return "paper wins"; 
     } 
     else { 
      return "scissors wins"; 
     } 
    } 
    else if(choice1 === scissors) { 
     if(choice2 === paper) { 
      return "scissors wins"; 
     } 
     else { 
      return "rock wins"; 
     } 
    } 
}; 
compare(userChoice, computerChoice); 
} 

,但它给我一个错误:不定义rock

+1

提示 - 岩石是不一样的“石头” – bsoist

+0

你还不明白什么部分错误的?你用字符串文字混淆变量吗? – SLaks

+0

如果你想避免所有的引用,你可以做一些像“rock =”rock“; paper =“paper”;剪刀=“剪刀”' – bsoist

回答

0

你已经错过了一些报价: -

var userChoices = ["rock", "paper", "scissors"]; 
var compare = function(choice1, choice2) { 
if(choice1 === choice2) { 
    return "It is a tie!"; 
} 
else if(choice1 === "rock") { 
if(choice2 === "paper") { 
    return "paper wins"; 
} 
else { 
    return "rock wins"; 
} 
} 
else if(choice1 === "paper") { 
if(choice2 === "rock") { 
    return "paper wins"; 
} 
else { 
    return "scissors wins"; 
} 
} 
else if(choice1 === "scissors") { 
if(choice2 === "paper") { 
    return "scissors wins"; 
} 
else { 
    return "rock wins"; 
} 
} 
}; 

for(i=0; i<userChoices.length; i++) { 
var userChoice = prompt("What would you like to select: rock, paper or scissors?"); 
var computerChoice = Math.random(); 
if(computerChoice<0.34) { 
    computerChoice = "rock"; 
} 
else if(computerChoice<=0.67) { 
computerChoice = "paper"; 
} 
else if(computerChoice<=1) { 
computerChoice = "scissors"; 
} 

var result = compare(userChoice, computerChoice); 
console.log("User: "+userChoice+", Computer: "+computerChoice+", Result: "+result); 
} 
+0

输出到您的程序来: 计算机:纸 计算机:岩石 计算机:剪刀 计算机:岩石 “这是一条领带! –

+0

实际上,我需要每个输入的输出,但程序只给出最后输入的输出。 –

+0

我修改了我的代码,现在您可以在每次迭代中看到结果,但是我仍然无法理解当您从提示中接受用户输入时数组的用户是什么。 – Indra

相关问题