我有一个简单的程序,可以在简单的Rock剪刀纸游戏中从用户和计算机获得一些输入。我应该比较用户的输入,然后与
的输出进行比较一切都很好,变量计算机显示正确的输出,但是当我分配给应该占用计算机第一个字符的变量cs时,它总是显示与播放器ps相同的输出,因此结果始终是平局,这是错误的。
你们任何人都可以告诉我为什么以及如何解决这个问题?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Complete Java Script Course</title>
<meta name="description" content="">
<meta name="author" content="Yuan">
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
</head>
<body>
<div id="output">Complete Java Script Course</div>
<script>
var output = document.getElementById("output");
var player = prompt("Do you Choose Rock, Paper or Scissors?")
var ps = player.charAt(0).toUpperCase();
var win = false;
var computer = Math.random();
if(computer < 0.34){computer ="Rock";}
else if (computer < 0.67){computer ="Paper";}
else{computer= "Scissors";}
var cs = computer.charAt(0).toUpperCase();
console.log(computer);
var html="";
if (cs=ps){
html+="Its a tie!";}
else{
if(ps=="R"){
if(cs=="S"){win=true;}
}
else if(ps=="P"){
if(cs=="R"){win=true;}
}
else{
if(cs=="P"){win=true;}
}
if(win){
html += "Player Wins!";
} else{
html+= "Computer Wins!";
}
}
output.innerHTML = "Player Vs Computer<br>"+ player+" "+ "vs" +" "+computer+"<br>Result:"+" "+html;
</script>
</body>
</html>
答案 0 :(得分:1)
你必须比较不分配。使用==
或更好===
:
if (cs == ps) {
分配时,您已将其设置为平局。这是您完全更正的代码 的片段:强>
var output = document.getElementById("output");
var player = prompt("Do you Choose Rock, Paper or Scissors?")
var ps = player.charAt(0).toUpperCase();
var win = false;
var computer = Math.random();
if (computer < 0.34) {
computer = "Rock";
} else if (computer < 0.67) {
computer = "Paper";
} else {
computer = "Scissors";
}
var cs = computer.charAt(0).toUpperCase();
console.log(computer);
var html = "";
if (cs == ps) {
html += "Its a tie!";
} else {
if (ps == "R") {
if (cs == "S") {
win = true;
}
} else if (ps == "P") {
if (cs == "R") {
win = true;
}
} else {
if (cs == "P") {
win = true;
}
}
if (win) {
html += "Player Wins!";
} else {
html += "Computer Wins!";
}
}
output.innerHTML = "Player Vs Computer<br>" + player + " " + "vs" + " " + computer + "<br>Result:" + " " + html;
&#13;
<div id="output">Complete Java Script Course</div>
&#13;
答案 1 :(得分:-1)
条件if (cs=ps)
将始终返回true,因为这是一个赋值而不是比较。设为if (cs===ps)
或if (cs==ps)
另外,如果您不了解这个概念,请参阅以下链接
答案 2 :(得分:-3)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Complete Java Script Course</title>
<meta name="description" content="">
<meta name="author" content="Yuan">
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
</head>
<body>
<div id="output">Complete Java Script Course</div>
<script>
var output = document.getElementById("output");
var player = prompt("Do you Choose Rock, Paper or Scissors?")
var ps = player.charAt(0).toUpperCase();
var win = false;
var computer = Math.random();
if(computer < 0.34){computer ="Rock";}
else if (computer < 0.67){computer ="Paper";}
else{computer= "Scissors";}
var cs = computer.charAt(0).toUpperCase();
console.log(computer);
var html="";
if (cs=ps){
html+="Its a tie!";}
else{
if(ps=="R"){
if(cs=="S"){win=true;}
}
else if(ps=="P"){
if(cs=="R"){win=true;}
}
else{
if(cs=="P"){win=true;}
}
if(win){
html += "Player Wins!";
} else{
html+= "Computer Wins!";
}
}
output.innerHTML = "Player Vs Computer<br>"+ player+" "+ "vs" +" "+computer+"<br>Result:"+" "+html;
</script>
</body>
</html>
亲爱的工作。经过测试,请再次检查