-3

我在这里遇到语法错误,但不明白为什么。TIA

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);

if(computerChoice >= 0.33) {
    computerChoice === "rock";
} else if ( computerChoice >= 0.34 && <= 0.66){
    computerChoice === "paper";
} else (computerChoice >= 0.67 && <= 1) {
    computerChoice === "scissors";
}
4

2 回答 2

4

嗯,这里有几个问题。

语法上:

} else ( computerChoice >= 0.67 && <= 1 ) {

应该

} else if ( computerChoice >= 0.67 && computerChoice <= 1 ) {

或者

} else {

但是在条件块中执行的东西实际上并没有做任何事情。您只是在测试一些东西是否相等,然后忽略测试结果。

我想你想要的更接近:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);

if ( computerChoice <= 0.33 ) {
    computerChoice = "rock";
} else if ( computerChoice >= 0.34 && computerChoice <= 0.66 ) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
于 2013-06-26T22:22:59.300 回答
1

您缺少computerChoice用于第二个逻辑比较的变量名 ( ) 和代码最后一部分中的else而不是。elseif此外,您在应该使用赋值的地方使用类型/值比较。

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);

if(computerChoice >= 0.33) {
    computerChoice = "rock";
} else if ( computerChoice >= 0.34 && computerChoice <= 0.66){
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}
于 2013-06-26T22:29:19.380 回答