如果/ Else / Switch返回错误结果 [英] If / Else / Switch returning the wrong results
问题描述
我正在使用ReactJS制作岩石剪刀布-蜥蜴-史波克(Big Bang Theory,电视节目),而且我遇到某种抽象问题。
I'm making a Rock-Paper-Scissors-Lizard-Spock (Big Bang Theory, the tv show) using ReactJS and i'm facing some kind of abstract issue.
switch (this.state.playerOnePick === 'Rock') {
case((this.state.playerTwoPick === 'Scissors') || (this.state.playerTwoPick === 'Lizard')):
return (
<div>
<h1>Player One wins !</h1>
<h2>P1: {this.state.playerOnePick} P2: {this.state.playerTwoPick}</h2>
</div>
);
break;
case((this.state.playerTwoPick === 'Paper') || (this.state.playerTwoPick === 'Spock')):
return (
<div>
<h1>Player Two wins !</h1>
<h2>P1: {this.state.playerOnePick}
P2: {this.state.playerTwoPick}</h2>
</div>
);
break;
}
switch (this.state.playerOnePick === 'Lizard') {
case((this.state.playerTwoPick === 'Spock') || (this.state.playerTwoPick === 'Paper')):
return (
<div>
<h1>Player One wins !</h1>
<h2>P1: {this.state.playerOnePick} P2: {this.state.playerTwoPick}</h2>
</div>
);
break;
case((this.state.playerTwoPick === 'Scissors') || (this.state.playerTwoPick === 'Rock')):
return (
<div>
<h1>Player Two wins !</h1>
<h2>P1: {this.state.playerOnePick} P2: {this.state.playerTwoPick}</h2>
</div>
);
break;
}
Rock vs Paper返回正确的结果,无论谁选择它,当P1:岩石,P2:蜥蜴,P1获胜时,但是当P1:蜥蜴P2:岩石,则返回P1获胜。.
Rock vs Paper is returning the right results, no matter who's picking it, when P1: Rock, P2: Lizard, P1 wins as expected, but when P1: Lizard P2: Rock, it is returning that P1 wins..
< a href = https://i.stack.imgur.com/80As0.png rel = nofollow noreferrer>当P1:蜥蜴P2:岩石
在任何地方,蜥蜴人都不应该赢得对岩石的胜利。
There is nowhere where Lizard is supposed to win vs Rock...
(玩家选择武器时,playerOnePick和playerTwoPick会正确更新)
(playerOnePick and playerTwoPick are correctly updated as the player pick a weapon)
推荐答案
switch
语句的正确用法是
switch (this.state.playerOnePick) {
case 'Rock':
switch (this.state.playerTwoPick) {
case 'Scissors'):
case 'Lizard':
return "Player One wins!";
break; // unnecessary after `return` but well
case 'Paper':
case 'Spock':
return "Player Two wins!";
break; // as above
}
break;
case 'Lizard':
switch (this.state.playerTwoPick) {
case 'Spock':
case 'Paper':
return "Player One wins!"
case 'Scissors':
case 'Rock':
return "Player Two wins!";
}
break;
}
显示的是 if
/ else
,其中包含许多布尔条件:
What you have shown is the layout for if
/else
, with lots of boolean conditions:
if (this.state.playerOnePick === 'Rock') {
if ((this.state.playerTwoPick === 'Scissors') || (this.state.playerTwoPick === 'Lizard')) {
return "Player One wins!";
} else if ((this.state.playerTwoPick === 'Paper') || (this.state.playerTwoPick === 'Spock')) {
return "Player Two wins!";
}
} else if (this.state.playerOnePick === 'Lizard') {
if ((this.state.playerTwoPick === 'Spock') || (this.state.playerTwoPick === 'Paper')) {
return "Player One wins!";
} else if ((this.state.playerTwoPick === 'Scissors') || (this.state.playerTwoPick === 'Rock')) {
return "Player Two wins!";
}
}
但是,实施Rock-Paper的真正问题是Scissors-Lizard-Spock就是所有重复项(这给错误留下了很大的空间)。实际的编程任务是弄清楚如何减少它。
提示:为每个可能的选择分配一个整数,然后进行一些数学运算。
编写一个单独的函数 winner(pick1,pick2)
当第一个玩家获胜时返回 -1
, 0
并列,第二名玩家获胜时, 1
。然后只需从与UI有关的ReactJS代码中调用它即可。
However, the real issue with implementing Rock-Paper-Scissors-Lizard-Spock is all that duplication (which leaves a lot room for error). The actual programming task is to figure out how to reduce that.
Tip: Assign each possible pick an integer number, and play around with some maths.
Write a separate function winner(pick1, pick2)
that returns -1
when the first player wins, 0
for a tie, and 1
when the second player wins. Then simply call that from the ReactJS code that is concerned with the UI stuff.
这篇关于如果/ Else / Switch返回错误结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!