石头,剪刀,布
Javascript Rock, Paper, Scissors
我遵循了http://www.codecademy.com上的Javascript教程,其中一部分是创建一个剪刀、石头、布游戏。在我完成它之后,我想我应该重新设计一下,让我自己使用。所以我这样做:
<!DOCTYPE html>
<html>
<body>
<script>
var userChoice = document.getElementById("userChoice").value;
if (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors"){
alert("Your choice was not rock, paper or scissors");
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";
}
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";
}
} else if (choice1 == "scissors") {
if (choice2 == "paper") {
return "Scissors win";
} else {
return "Rock wins";
}
}
}
function Run() {
document.getElementById("pcc").innerHTML = "Computer: " + computerChoice;
document.getElementById("result").innerHTML = compare(userChoice, computerChoice);
}
</script>
<main>
<h2>Choose between rock, paper or scissors: </h2>
<input id="userChoice">
<button onClick="Run()">Choose</button>
<p id="pcc"></p>
<p id="result"></p>
</main>
</body>
</html>
但是它不起作用,我得到未定义的比较,computerChoice, userChoice,并且没有任何东西存储在值中。怎么了?
您需要将脚本的所有内容放在一个函数中,试试这个。它试图在开始的逻辑设置变量之前使用它们,因为开始的逻辑从未被调用:)
function run(){
var userChoice = document.getElementById("userChoice").value;
if (userChoice != "rock" && userChoice != "paper" && userChoice != "scissors"){
alert("Your choice was not rock, paper or scissors");
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";
}
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";
}
} else if (choice1 == "scissors") {
if (choice2 == "paper") {
return "Scissors win";
} else {
return "Rock wins";
}
}
}
document.getElementById("pcc").innerHTML = "Computer: " + computerChoice;
document.getElementById("result").innerHTML = compare(userChoice, computerChoice);
}
您应该删除<main>
标记并正确关闭第二段标记,如下所示:
<p id="result"></p>
相关文章:
- 更多的石头剪刀
- 新手Javascript函数返回未定义的石头剪刀游戏
- 编写石头,纸,剪刀游戏
- 代码学院的石头剪刀布
- 石头剪刀布游戏功能在Javascript
- 如何在石头剪刀游戏中延迟回答
- SyntaxError:石头剪刀中的意外标记{
- 石头剪刀布蜥蜴斯波克游戏逻辑偶尔会输出错误的赢家,我不知道为什么
- Javascript中的石头剪刀布
- 尝试为石头剪刀布游戏显示图像而不是字母
- 在 JavaScript 中构建“石头、纸、剪刀”
- 石头剪刀布 JavaScript 问题
- 两个玩家石头剪刀布在JavaScript
- 石头,纸,剪刀jQuery游戏不工作
- 石头、纸、剪刀游戏动画
- 我可以'我不明白为什么得分不高;我不在玩石头、布、剪刀的游戏
- 石头剪刀游戏的回合计数(潜在的jquery问题)
- 退出Javascript中的函数.简单的石头,纸,剪刀游戏
- 石头纸,剪刀游戏
- 如何让我的剪刀石头布游戏刷新上按一下按钮