我正在使用ReactJS制作岩石剪刀布-蜥蜴-Spock(大爆炸理论,电视节目),而我正面临某种抽象问题。

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;

}


无论P1:Rock,P2:Lizard,P1均按预期获胜,但Rock vs Paper均返回正确的结果,而P1:Lizard P2:Rock时,P1获胜。

What it returns me when P1:Lizard P2:Rock

蜥蜴在任何地方都无法赢得对岩石的胜利...

(随着玩家选择武器,playerOnePick和playerTwoPick会正确更新)

最佳答案

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的布局,其中包含许多布尔条件:

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代码中调用即可。

关于javascript - 如果/Else/Switch返回错误结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42520994/

10-09 20:30