var rock = rock;
var paper= paper;
var snip = snip;
var playerOneName= prompt("what is your name");
playerOne = choice();
var playerTwoName = prompt("what is your name");
playerTwo=choice();
function choice(pick){
prompt("rock paper or snip");
}
if(playerOne === rock && playerTwo === snip){
alert(playerOneName + " " + "Wins");
}else if(playerOne === paper && playerTwo === rock){
alert(playerOneName + " " + "Wins");
}else if(playerOne === snip && playerTwo === paper){
alert(playerOneName + " " + "Wins");
}else if(playerTwo === rock && playerOne === snip){
alert(playerTwoName + " " + "Wins");
}else if(playerTwo === paper && playerOne === rock){
alert(playerTwoName + " " + "Wins");
}else if(playerTwo === snip && playerOne === paper){
alert(playerTwoName + " " + "Wins");
}else if(playerOne === playerTwo){
alert("it's a tie try again");
}
<!doctype html>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="rps.js"></script>
</head>
<body>
<ul>
<li>Rock</li>
<li>Paper</li>
<li>Snip</li>
</ul>
</body>
</html>
大家好, 我带着一个非常新的问题回来了。出于某种原因,当我为这个摇滚剪刀游戏运行我的代码时...它经常告诉我玩家赢了。我发现的问题是它始终选择if语句而不是else if。如果有人可以提供帮助那就太棒了! 再次感谢你的时间。这也是我学习javascript的第一周......有点可悲的是,很多人都会对我的问题进行投票...这就是为什么大多数人都不敢问问题的原因... < / p>
答案 0 :(得分:0)
您需要return
来自window.prompt的输入值。即
function choice(pick){
var answer = prompt("rock paper or snip");
return answer;
}
这样您就可以获得用户在窗口提示中输入的内容。在某些情况下,您可能需要检查null
值。
更多阅读at Mozilla Docs
答案 1 :(得分:0)
尝试使用此更正并验证的JavaScript代码 -
#include <iostream>
#include <stdio.h>
#include <stdint.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class FooBar{
public:
typedef void(FooBar::*OnDio)(void);
void OnDio0Irq( void ){
printf("dio0\n");
};
void OnDio1Irq( void ){
printf("dio1\n");
};
FooBar(){
OnDio dioArray[] = {&OnDio0Irq, &OnDio1Irq};
};
OnDio *dioArray[2];
private:
};
int main(int argc, char* argv[]){
typedef void(FooBar::*OnDio)(void);
void (FooBar::*foo)(void);
OnDio *myPtr;
FooBar *fb = new FooBar();
myPtr = *(&fb->dioArray[0]);
foo = (OnDio &)(myPtr[0]);
(foo)();//me need call fb->dioArray[0]()
(*myPtr)(); // ?
}
更正 -
var rock = rock;
var paper= paper;
var snip = snip;
var playerOneName= prompt("what is your name");
playerOne = choice();
var playerTwoName = prompt("what is your name");
playerTwo=choice();
alert('Player 1 Chose - '+ playerOne + 'Player 2 Chose - '+playerTwo);
function choice(pick){
return(prompt("rock paper or snip")); //return the prompt value
}
if(playerOne === 'rock' && playerTwo === 'snip'){
alert(playerOneName + ' ' + 'Wins');
}else if(playerOne === 'paper' && playerTwo === 'rock'){
alert(playerOneName + ' ' + 'Wins');
}else if(playerOne === 'snip' && playerTwo === 'paper'){
alert(playerOneName + ' ' + 'Wins');
}else if(playerTwo === 'rock' && playerOne === 'snip'){
alert(playerTwoName + ' ' + 'Wins');
}else if(playerTwo === 'paper' && playerOne === 'rock'){
alert(playerTwoName + ' ' + 'Wins');
}else if(playerTwo === 'snip' && playerOne === 'paper'){
alert(playerTwoName + ' ' + 'Wins');
}else if(playerOne === playerTwo){
alert('its a tie try again');
}
函数中的返回值。提示具有在函数中接收和返回的返回值。