我可以'我不明白为什么得分不高;我不在玩石头、布、剪刀的游戏
I can't figure out why the scoring isn't working in my rock, paper, scissors game
游戏运行良好,只是分数不会增加,我的"裁判"框也不会显示我根据结果分配的文本。
我使用if-else语句随机分配计算机选项,替换图像并相应地设置computerChoice变量。我为用户输入使用了and if elsee语句来分配正确的图像并设置userChoice变量。然后,我使用了一组单独的嵌套if-else语句来比较computerCoice和userChoice变量,并在裁判框中输出文本,以及在获胜者得分框中输入1分,但这部分不起作用。有什么想法吗?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rock, Paper, Scissors!</title>
<style>
.clear {clear: both;}
.box {width: 160px; height: 150px; float: left; border: 4px solid #000;}
.wrap {width: 110px; float: left;}
.wrap input {width: 85px;}
form {width: 340px; margin: 20px auto 0 auto;}
</style>
<script>
var referee = document.getElementById('ref').value;
var userScore = document.getElementById('userScore').value;
var computerScore = document.getElementById('compScore').value;
userScore = 0;
computerScore = 0;
function startGame() {
var computerChoice = Math.random();
if (computerChoice >= 0 && computerChoice <= .33) {
computerChoice = "rock"
document.getElementById("compPic").src = "rock.jpg";
} else if (computerChoice >= .34 && computerChoice <= .66) {
computerChoice = "paper"
document.getElementById("compPic").src = "paper.jpg";
}else if (computerChoice >= .67 && computerChoice <= 1){
computerChoice = "scissors"
document.getElementById("compPic").src = "scissors.jpg";
};
var userChoice = document.getElementById('userChoice').value;
if (userChoice === "rock" || userChoice === "Rock") {
userChoice = "rock";
document.getElementById("userPic").src = "rock.jpg";
} else if (userChoice === "paper" || userChoice === "Paper") {
userChoice = "paper";
document.getElementById("userPic").src = "paper.jpg";
} else if (userChoice === "scissors" || userChoice === "Scissors") {
userChoice = "scissors";
document.getElementById("userPic").src = "scissors.jpg";
};
if (userChoice === computerChoice) {
referee = "Tie!";
}else if (userChoice === 'rock') {
if (computerChoice === 'paper') {
referee = 'Computer Wins!';
computerScore = ++computerScore;
} else if (computerChoice === 'scissors') {
referee = 'You Wins!';
userScore = ++userScore;
};
}else if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
referee = 'Computer Wins!';
computerScore = ++computerScore;
} else if (computerChoice === 'rock') {
referee = 'You Wins!';
userScore = ++userScore;
};
}else if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
referee = 'Computer Wins!';
computerScore = ++computerScore;
} else if (computerChoice === 'paper') {
referee = 'You Wins!';
userScore = ++userScore;
};
};
}
</script>
</head>
<body>
<form>
<div class="box"><img id="userPic" src="rps.jpg"></div>
<div class="box"><img id="compPic" src="rps.jpg"></div>
<br class="clear"><br>
<div class="wrap">
<label for="userScore">User Score:</label><br>
<input type="text" name="userScore" id="userScore" value="" readonly>
</div>
<div class="wrap">
<label for="ref">Referee:</label><br>
<input type="text" name="ref" id="ref" placeholder="Referee's Call..." value="" readonly>
</div>
<div class="wrap">
<label for="compScore">Computer Score:</label><br>
<input type="text" name="compScore" id="compScore" value="" readonly>
</div>
<br class="clear"><br>
<label for="userChoice">Choose Your Weapon:</label><br>
<input type="text" name="userChoice" id="userChoice" placeholder="Rock, Paper, or Scissors?">
<input type="button" value="Battle!" onclick="startGame()">
</form>
</body>
</html>
您必须将用户分数右对齐到用户分数输入字段。第二,你的得分将在每场比赛中重置。。它不是可交换的。。我不希望这个或它被你错过,因为它是交换的。。define userScore=0;computerScore=0;在函数定义之前。
试试这个
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rock, Paper, Scissors!</title>
<style>
.clear {clear: both;}
.box {width: 160px; height: 150px; float: left; border: 4px solid #000;}
.wrap {width: 110px; float: left;}
.wrap input {width: 85px;}
form {width: 340px; margin: 20px auto 0 auto;}
</style>
<script>
//var referee = document.getElementById('ref').value;
//var userScore = document.getElementById('userScore').value;
//var computerScore = document.getElementById('compScore').value;
userScore = 0;
computerScore = 0;
function startGame() {
var computerChoice = Math.random();
if (computerChoice >= 0 && computerChoice <= .33) {
computerChoice = "rock"
//document.getElementById("compPic").src = "rock.jpg";
} else if (computerChoice >= .34 && computerChoice <= .66) {
computerChoice = "paper"
//document.getElementById("compPic").src = "paper.jpg";
}else if (computerChoice >= .67 && computerChoice <= 1){
computerChoice = "scissors"
//document.getElementById("compPic").src = "scissors.jpg";
};
var userChoice = document.getElementById('userChoice').value;
if (userChoice === "rock" || userChoice === "Rock") {
userChoice = "rock";
//document.getElementById("userPic").src = "rock.jpg";
} else if (userChoice === "paper" || userChoice === "Paper") {
userChoice = "paper";
// document.getElementById("userPic").src = "paper.jpg";
} else if (userChoice === "scissors" || userChoice === "Scissors") {
userChoice = "scissors";
// document.getElementById("userPic").src = "scissors.jpg";
};
if (userChoice === computerChoice) {
referee = "Tie!";
}else if (userChoice === 'rock') {
if (computerChoice === 'paper') {
referee = 'Computer Wins!';
computerScore = ++computerScore;
} else if (computerChoice === 'scissors') {
referee = 'You Wins!';
userScore = ++userScore;
};
}else if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
referee = 'Computer Wins!';
computerScore = ++computerScore;
} else if (computerChoice === 'rock') {
referee = 'You Wins!';
userScore = ++userScore;
};
}else if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
referee = 'Computer Wins!';
computerScore = ++computerScore;
} else if (computerChoice === 'paper') {
referee = 'You Wins!';
userScore = ++userScore;
};
};
document.getElementById('ref').value = referee;
document.getElementById('userScore').value = userScore;
document.getElementById('compScore').value =computerScore;
}
</script>
</head>
<body>
<form>
<div class="box"><img id="userPic" src="rps.jpg"></div>
<div class="box"><img id="compPic" src="rps.jpg"></div>
<br class="clear"><br>
<div class="wrap">
<label for="userScore">User Score:</label><br>
<input type="text" name="userScore" id="userScore" value="" readonly>
</div>
<div class="wrap">
<label for="ref">Referee:</label><br>
<input type="text" name="ref" id="ref" placeholder="Referee's Call..." value="" readonly>
</div>
<div class="wrap">
<label for="compScore">Computer Score:</label><br>
<input type="text" name="compScore" id="compScore" value="" readonly>
</div>
<br class="clear"><br>
<label for="userChoice">Choose Your Weapon:</label><br>
<input type="text" name="userChoice" id="userChoice" placeholder="Rock, Paper, or Scissors?">
<input type="button" value="Battle!" onclick="startGame()">
</form>
</body>
</html>
相关文章:
- 新手Javascript函数返回未定义的石头剪刀游戏
- 编写石头,纸,剪刀游戏
- 石头剪刀布游戏功能在Javascript
- 如何在石头剪刀游戏中延迟回答
- 石头剪刀布蜥蜴斯波克游戏逻辑偶尔会输出错误的赢家,我不知道为什么
- 尝试为石头剪刀布游戏显示图像而不是字母
- 石头,纸,剪刀jQuery游戏不工作
- 石头、纸、剪刀游戏动画
- 我可以'我不明白为什么得分不高;我不在玩石头、布、剪刀的游戏
- 石头剪刀游戏的回合计数(潜在的jquery问题)
- 退出Javascript中的函数.简单的石头,纸,剪刀游戏
- 石头纸,剪刀游戏
- 如何添加图片到石头,剪刀游戏
- javascript,创建一个石头、纸、剪刀游戏
- 编程一个石头剪刀html游戏
- 玩石头剪刀游戏
- 如何使石头,纸,剪刀游戏重复
- 石头剪刀布游戏错误,Javascript
- 如果,则石头剪刀布游戏
- 石头剪刀布Javascript游戏添加选择限制