我的 switch 语句格式是否错误?
我一直在做一个“剪刀石头布”游戏,看看我是否了解 JavaScript 的基础知识。一切都运行良好,直到我到达 fight()
中第 30 行到第 71 行的 if 语句:我已经调整了应答变量以指示它如何“if'ing”。它似乎返回整个“if”中每个子“if”的默认值。我是否在次要条件中使用了错误的变量?
radioGroup.html
<script type = "text/javascript">
//<![CDATA[
// from radioGroup.html
function fight(){
var randomChoice = Math.random();
var threeChoice = Math.floor (randomChoice * 3);
var weapon = document.getElementsByName("weapon");
for (i = 0; i < weapon.length; i++){
currentWeapon = weapon[i];
if (currentWeapon.checked){
var selectedWeapon = currentWeapon.value;
} // end if
} // end for
var cpuChoice = new Array("Paper", "Rock", "Scissors")
var reply = "xxx"
if (threeChoice === 0) {
if (weapon === "Paper") {
reply = "11";
}
else if (weapon === "Rock") {
reply = "12";
}
else if (weapon === "Scissors") {
reply = "13";
}
else {
reply = "what1";
}
}else if (threeChoice === 1) {
if (weapon === "Paper") {
reply = "21";
}
else if (weapon === "Rock") {
reply = "22";
}
else if (weapon === "Scissors") {
reply = "23";
}
else {
reply = "what2";
}
}else if (threeChoice === 2) {
if (weapon === "Paper") {
reply = "31";
}
else if (weapon === "Rock") {
reply = "32";
}
else if (weapon === "Scissors") {
reply = "33";
}
else {
reply = "what3";
}
}else {
reply = "hay now?";
}
var output = document.getElementById("output");
var response = "<h2>You have chosen ";
response += selectedWeapon + "<h2>The CPU has chosen ";
"<\/h2> \n";
response += cpuChoice[threeChoice] + "<\/h2> \n";
response += reply + "<\/h2> \n";
output.innerHTML = response;
} // end function
//]]>
</script>
</head>
<body>
<h1>Choose!</h1>
<form action = "">
<fieldset>
<input type = "radio"
name = "weapon"
id = "radPaper"
value = "Paper"
checked = "checked" />
<label for = "radPaper">Paper</label>
<input type = "radio"
name = "weapon"
id = "radRock"
value = "Rock" />
<label for = "radRock">Rock</label>
<input type = "radio"
name = "weapon"
id = "radScissors"
value = "Scissors" />
<label for = "radScissors">Scissors</label>
<button type = "button"
onclick = "fight()">
fight the dragon
</button>
</fieldset>
</form>
<div id = "output">
</div>
</body>
</html>
I have been working on a "Rock Paper Scissors" game to see if I understand the basics of JavaScript. It all works well until I get to the if statement from lines 30 to 71 in the fight()
: I have adjusted the reply variable to indicate how it may be "if'ing" through. It seems to be returning the default value of every sub "if" in the overall "if". Am I using the wrong variable for the secondary condition maybe?
radioGroup.html
<script type = "text/javascript">
//<![CDATA[
// from radioGroup.html
function fight(){
var randomChoice = Math.random();
var threeChoice = Math.floor (randomChoice * 3);
var weapon = document.getElementsByName("weapon");
for (i = 0; i < weapon.length; i++){
currentWeapon = weapon[i];
if (currentWeapon.checked){
var selectedWeapon = currentWeapon.value;
} // end if
} // end for
var cpuChoice = new Array("Paper", "Rock", "Scissors")
var reply = "xxx"
if (threeChoice === 0) {
if (weapon === "Paper") {
reply = "11";
}
else if (weapon === "Rock") {
reply = "12";
}
else if (weapon === "Scissors") {
reply = "13";
}
else {
reply = "what1";
}
}else if (threeChoice === 1) {
if (weapon === "Paper") {
reply = "21";
}
else if (weapon === "Rock") {
reply = "22";
}
else if (weapon === "Scissors") {
reply = "23";
}
else {
reply = "what2";
}
}else if (threeChoice === 2) {
if (weapon === "Paper") {
reply = "31";
}
else if (weapon === "Rock") {
reply = "32";
}
else if (weapon === "Scissors") {
reply = "33";
}
else {
reply = "what3";
}
}else {
reply = "hay now?";
}
var output = document.getElementById("output");
var response = "<h2>You have chosen ";
response += selectedWeapon + "<h2>The CPU has chosen ";
"<\/h2> \n";
response += cpuChoice[threeChoice] + "<\/h2> \n";
response += reply + "<\/h2> \n";
output.innerHTML = response;
} // end function
//]]>
</script>
</head>
<body>
<h1>Choose!</h1>
<form action = "">
<fieldset>
<input type = "radio"
name = "weapon"
id = "radPaper"
value = "Paper"
checked = "checked" />
<label for = "radPaper">Paper</label>
<input type = "radio"
name = "weapon"
id = "radRock"
value = "Rock" />
<label for = "radRock">Rock</label>
<input type = "radio"
name = "weapon"
id = "radScissors"
value = "Scissors" />
<label for = "radScissors">Scissors</label>
<button type = "button"
onclick = "fight()">
fight the dragon
</button>
</fieldset>
</form>
<div id = "output">
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您关于错误使用开关条件的说法是正确的,“case”是开关中表达式的计算结果。
你应该使用 if 语句,但你可以使用这样的 switch 语句
You are correct about using the switch condition incorrectly, the "case" is what the expression in your switch evaluates to.
you should be using if statements, but you can use a switch statement like this
case
用于测试简单值,通常是数字。对于你的情况,我建议为石头、剪刀、布分配数字,这样这些数字的顺序就可以清楚地表明谁赢了。例如,石头=0,布=1,剪刀=2。然后,(3 + (a - b)) % 3
会告诉您谁赢了:0 表示平局,1 表示a
获胜,2 表示b
赢了。然后,您可以对该操作的结果执行简单的三例切换
。case
is for testing simple values, usually numeric. For your case, I'd suggest assigning numbers to rock, paper, and scissors, such that the ordering of those numbers makes it clear who won. For example, rock=0, paper=1, scissors=2. Then,(3 + (a - b)) % 3
will tell you who won: 0 is a tie, 1 indicates thata
won, and 2 indicates thatb
won. Then you can do a simple three-caseswitch
over the result of that operation.