我要在JavaScript中正确嵌套这些if / else if语句吗?

时间:2019-02-25 04:56:08

标签: javascript if-statement nested conditional

我正在用JavaScript进行石头,纸,剪刀的游戏,我必须让用户知道是否有领带,并且除了石头,纸和剪刀之外是否存在无效的用户选择,所以我想嵌套if / else语句将是合适的。我对JS很陌生,无法确定我的代码不起作用的原因是由于错误的嵌套还是其他原因。我得到的只是一个意外令牌的解析错误。

var UserChoice = window.prompt("Select rock, paper, or scissors");

var computChoice = Math.random();
if (computChoice <= 0.33) {
    computChoice = "scissors";
} else if (computChoice <= 0.66 && computChoice > 0.33) {
    computChoice = "paper";
} else {
    computChoice = "rock";
}

if (UserChoice === "paper") {
    if (UserChoice === "paper" && computChoice === "rock") {
        window.alert("You chose paper and the computer chose rock! You win! Paper covers rock");  
    } else if (UserChoice === "paper" && computChoice === "paper") {
        window.alert("It's a tie!");
    } else if (UserChoice === "paper" && computChoice === "scissors") {
        window.alert("You lose! You chose paper and computer chose scissors. Scissors cut paper!");
    } 
} else if (UserChoice === "scissors") {
    if (UserChoice === "scissors" && computChoice === "paper") {
        window.alert("You chose scissors and the computer chose paper! You win! Scissors cut paper.");
    } else if (UserChoice === "scissors" && computChoice === "scissors") {
        window.alert("It's a tie! You chose scissors and the computer chose scissors!");
    } else if (UserChoice === "scissors" && computChoice === "rock") {
        window.alert("You lose! You chose scissors and computer chose rock. Rock smashes scissors!");
    }
} else if (UserChoice === "rock") {
    if (UserChoice === "rock" && computChoice === "scissors") {
        window.alert("You chose rock and the computer chose scissors! You win! Rock smashes scissors."); 
    } else if (UserChoice === "rock" && computChoice === "rock") {
        window.alert("It's a tie! You chose rock and the computer chose rock!");
    } else if (UserChoice === "scissors" && computChoice === "rock") {
        window.alert("You lose! You chose rock and computer chose paper. Paper covers rock!");
} else {
    window.prompt("Invalid choice! Choose from rock, paper, or scissors");
}

2 个答案:

答案 0 :(得分:1)

您的问题与您的代码无关。这是一个简单的语法错误

在结尾处缺少C1.CurrencyRate / C2.CurrencyRate AS USCurrency来关闭外部if语句。我更新了您的代码段以使其在下面运行。

winSize = (32,32)
blockSize = (16,16)
blockStride = (8,8)
cellSize = (8,8)
nbins = 9
hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins)
h = hog.compute(img_gray)
print(h) 

答案 1 :(得分:0)

在这种情况下,您只需在代码末尾添加}

但是还有其他创建rock, paper, scissors game的方法:

var UserChoice = window.prompt("Select rock, paper, or scissors");

var computChoice = Math.random();

var computChoice = computChoice < 0.33 ? "scissors" : ( computChoice > 0.66 ? "rock" : "paper" ) ;

var mess = { 
  rock : { scissors : 'You Win!, Rock smashes scissors!', paper : 'You lose!, Paper covers rock!'} ,
  paper : { rock : 'You Win!, Paper covers rock!', scissors : 'You lose!, Scissors cut paper!' },
  scissors : { paper : 'You Win!, Scissors cut paper!', rock : 'You lose!, Rock smashes scissors!' }
}

if ( computChoice === UserChoice)
  result = "It's a tie!" ; 
	 
else if ( UserChoice !== "rock" && UserChoice !== "paper" && UserChoice !== "scissors" )
  result = "Invalid choice! Choose from rock, paper, or scissors" ;

else
  result = mess[UserChoice][computChoice] ;

console.log( 'you chose ' + UserChoice + ' and computer chose ' + computChoice + ' ( ' + result + ' ) ') ;