我现在已经被困在7/9两晚了。这是一款摇滚纸剪刀游戏。我无法弄清楚出了什么问题。我尝试了在线lint,它还说我的第22行是一个错误(预期标识符,而不是'else')。按照说明,如果在比较函数中的现有代码下,我写了另一个。
我的代码:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice ="rock"
} else if(computerChoice <= 0.67) {
computerChoice ="paper";
} else {
computerChoice ="scissors";
} console.log("Computer: " + computerChoice);
var compare=function(choice1, choice2){
if(choice1 === choice2) {
return("The result is a tie!");
}
else if(choice1 ==="rock"){
if(choice2 ==="scissors")
return("rock wins");
}
else{
return"paper wins";
}
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}
答案 0 :(得分:1)
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}
您在使用else if
;
它应该是:
else if(choice1 ==="paper"){
if(choice2 ==="rock")
return("paper wins");
}
答案 1 :(得分:1)
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice ="rock"
} else if(computerChoice <= 0.67) {
computerChoice ="paper";
} else {
computerChoice ="scissors";
} console.log("Computer: " + computerChoice);
var compare=function(choice1, choice2){
if(choice1 === choice2) {
return("The result is a tie!");
}
else if(choice1 ==="rock"){
if(choice2 ==="scissors")
return("rock wins");
}
else{
return"paper wins";
}
else if(choice1 ==="paper");{ -- on this there is semicolon after elseif block.. and how come else if is there after else block..
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}
答案 2 :(得分:1)
我假设你是初学者。编写一个非常干净的代码,处理空格和制表符,这是解决调试问题的最佳方法。第22行确实存在问题,你在条件语句后放了一个分号。
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if(computerChoice < 0.34) {
computerChoice = "rock"
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return("The result is a tie!");
} else if(choice1 === "rock") {
if(choice2 === "scissors")
return("rock wins");
} else {
return "paper wins";
} else if(choice1 === "paper"){//here the error was.
if(choice2 === "rock")
return("paper wins");
} else {
return "scissors wins";
}
}
答案 3 :(得分:0)
我在您的代码中看到了几个语法错误。它应如下所示:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return("The result is a tie!");
} else if(choice1 === "rock") {
if(choice2 === "scissors") {
return("rock wins");
} else {
return "paper wins";
}
} else if(choice1 ==="paper") {
if(choice2 ==="rock") {
return("paper wins");
} else {
return"scissors wins";
}
}
}
答案 4 :(得分:0)
else if
语句后您无法else
,这就是导致错误消息的原因error(Expected an identifier and instead saw 'else')
当然,在该行真正结束之前没有分号
https://jsfiddle.net/8hcpfnhw/
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice ="rock"
} else if(computerChoice <= 0.67) {
computerChoice ="paper";
} else {
computerChoice ="scissors";
} console.log("Computer: " + computerChoice);
var compare=function(choice1, choice2){
if(choice1 === choice2) {
return("The result is a tie!");
}
else if(choice1 ==="rock"){
if(choice2 ==="scissors")
return("rock wins");
}
else if(choice1 === "paper"){ // I changed this to else if instead of else
return "paper wins";
}
else if(choice1 ==="paper") {
if(choice2 ==="rock")
return("paper wins");
}
else{ // only else as last check
return"scissors wins";
}
}