我正在JS制作一个简单的摇滚剪刀游戏;
我按照我的任务做到了,但我想改进它。我想禁止用户在提示窗口中放置“摇滚”“纸板”或“剪刀”之外的任何内容。没有注意天气,它是大写或小写。
除了过滤掉所有不需要的单词外,一切正常。
我很确定问题是在while行中。如果我删除了所有内容并且只留下,比如说papper,它就会起作用a.k.a.在我写papper之前它不接受任何东西,但如果我想用所有这些来做这件事,它不允许我输入任何东西。它不断刷新。
任何想法? :)
alert("Lets play rock, papper scissors!");
//******COMPUTER'S PICK
var cPick = Math.floor(Math.random() * 3) + 1;
switch(cPick){
case 1:
cPick = "rock";
break;
case 2:
cPick = "papper";
break;
case 3:
cPick = "scissors";
break;
}
console.log(cPick);
//******YOUR PICK
var isValid = false;
//*********
var yourPick;
do{
yourPick = prompt("Your pick! Choose Rock, Papper of Scissors");
yourPick = yourPick.toLowerCase();
console.log(yourPick);
} while ((yourPick !=="papper") || (yourPick!=="rock") || (yourPick!=="scissors"));
if (yourPick == cPick){
document.getElementById("demo").innerHTML = ("You both chose " + yourPick + "! It's a tie.");
} else if (yourPick == "rock" && cPick == "scissors"){
document.getElementById("demo").innerHTML = ("You've picked " + yourPick + ". Computer choose " + cPick + "! You win!");
} else if (yourPick == "scissors" && cPick == "papper"){
document.getElementById("demo").innerHTML = ("You've picked " + yourPick + ". Computer choose " + cPick + "! You win!");
} else if (yourPick == "papper" && cPick == "rock"){
document.getElementById("demo").innerHTML = ("You've picked " + yourPick + ". Computer choose " + cPick + "! You win!");
} else if (yourPick == "rock" && cPick == "papper"){
document.getElementById("demo").innerHTML = ("You've picked " + yourPick + ". Computer choose " + cPick + "! You loose!");
} else if (yourPick == "scissors" && cPick == "rock"){
document.getElementById("demo").innerHTML = ("You've picked " + yourPick + ". Computer choose " + cPick + "! You loose!");
} else if (yourPick == "papper" && cPick == "scissors"){
document.getElementById("demo").innerHTML = ("You've picked " + yourPick + ". Computer choose " + cPick + "! You loose!");
}
.container{
width: 80%;
background-color: cyan;
border: 4px solid blue;
color: black;
margin: 20rem auto;
text-align: center;
}
#demo{
font-size: 2rem;
font-weight: 600;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="container">
<p id="demo"></p>
</div>
<script src="script.js" type="text/javascript"></script>
</body>
</html>
答案 0 :(得分:1)
您需要&&
来结合(负面)条件,而不是||
,因为您的
(x != A) || (x != B) || (x != C)
与:
相同!(x == A) || !(x == B) || !(x == C)
与:
相同!(x == A && x == B && x == C)
最后一步来自De Morgan's Laws。
由于您的字符串不可能同时等于A,B和C,因此表达式总是!false
(即true
)并且while
循环永远不会终止。
答案 1 :(得分:0)
您需要使用&&
代替||
,因为您想要提示用户,而不是岩石和纸和剪刀
答案 2 :(得分:0)
此条件始终为真,因此您永远不会突破while循环。
(yourPick !=="papper") || (yourPick!=="rock") || (yourPick!=="scissors")
因为您正在使用逻辑OR,所以只有表达式的一部分必须为true才能使整个事物成立。例如,即使用户输入&#34; papper&#34;,它仍然是&#34; papper&#34;不等于&#34;摇滚&#34;。所以表达式总体上评估为真。
您需要&&
代替:
(yourPick !=="papper") && (yourPick!=="rock") && (yourPick!=="scissors")
答案 3 :(得分:0)
while ((yourPick !=="papper") && (yourPick!=="rock") && (yourPick!=="scissors"));
使用&&
代替||
; - )