我已经检查过这一天并且整天都在研究它。问题出于某种原因,除string
var result
的{{1}}值外,我无法获得任何结果。
有人可以看一下这个并告诉我如何修复它,以便在适当时使用var的其他字符串值吗?
'the result is a tie!'
答案 0 :(得分:0)
choice1
和choice2
似乎未在此代码中定义,
如果这是真的,你基本上是在打电话
if(undefined === undefined) doThings;
一个简单的解决方案就是改变
choice1
至userChoice
和
choice2
至computerChoice
另外,不确定这是否相关,但是,您将computerChoice
定义为数字,然后将其设置为字符串。
答案 1 :(得分:0)
您在代码中使用不同的变量:userChoice和computerChoice针对choice1和choice2 如果你使这兼容,它几乎可以工作。我对你的代码做的唯一的改进:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice <= 0.25) {
computerChoice = "rock";
} else if (computerChoice <= 0.50) {
computerChoice = "paper";
} else if (computerChoice <= 0.75) {
computerChoice = "scissors";
} else {
computerChoice = "Arnold Schwarzenegger";
}
choice1=userChoice;
choice2=computerChoice;
if (choice1 === choice2) {
var result = "The result is a tie!";
}
else if (choice1 === "rock") {
if (choice2 === "scissors") {
var result = "rock Wins";
}
else if (choice2 === "paper") {
var result = "paper Wins";
}
else if (choice2 = "Arnold Schwarzenegger") {
var result = "You have been TERMINATED";
}
}
else if (choice1 === "scissors") {
if (choice2 === "rock") {
var result = "rock Wins";
}
else if (choice2 === "paper") {
var result = "scissors Wins";
}
else if (choice2 = "Arnold Schwarzenegger") {
var result = "You have been TERMINATED";
}
}
else if (choice1 === "paper") {
if (choice2 === "scissors") {
var result = "scissors Wins";
}
else if (choice2 === "rock") {
var result = "paper Wins";
}
else if (choice2 = "Arnold Schwarzenegger") {
var result = "You have been TERMINATED";
}
}
else if (choice1 === "Arnold Schwarzenegger") {
if (choice2 === "scissors") {
var result = "Get to the Chopper!";
}
else if (choice2 === "rock") {
var result = "Hasta la vista, baby!";
}
else if (choice2 = "paper") {
var result = "I'll be back";
}
}
alert(result);
你总是得到答案“结果是平局!”因为两个变量choice1和choice2都是(使用你的代码)没有实例化,所以值为null,所以它们是相等的。