带开关的多个变量(Javascript)

时间:2017-08-01 23:27:12

标签: javascript switch-statement

我正在尝试创建一个基本的Rock,Paper,Scissors游戏。 因为每回合都有相当多的条件,所以我想使用switch代替if& else。 我的代码是:

var computerChoice = Math.floor(Math.random() * 3)
var humanChoice = prompt('1 = Rock | 2 = Paper | 3 = Scissors')
        console.log(humanChoice);
        console.log(computerChoice);
    switch (computerChoice, humanChoice) {
        case 1 , 1:
            confirm('ROCK VS ROCK its a draw!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case 1 , 2:
            confirm('Paper win against rock! Human won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case 1 , 3:
            confirm('Rock win against scissors! Computer won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case 2 , 1:
            confirm('Paper win against rock! computer won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case 2 , 2:
            confirm('PAPER VS PAPER its a draw!!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case  2 , 3:
            confirm('scissors win against paper! Human won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case  3 , 1:
            confirm('Rock win against scissors! Human won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case  3 , 2:
            confirm('scissors win against Paper! Computer won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case  3 , 3:
            confirm('SCISSORS VS SCISSORS its a draw!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        default:
        confirm("I don't remember telling you that " + humanChoice + " is an option.");
        break;
    }; 

代码实际上跳转到默认值,这非常糟糕。 我究竟做错了什么?我想这是我在案例中编写条件的方式,但即使在搜索Google之后,我也无法使其发挥作用。

0 个答案:

没有答案