使用开关时出现意外令牌?

时间:2018-08-04 15:24:28

标签: javascript switch-statement

试图在剪刀石头布游戏中使用switch语句,但是不断得到意料之外的标记,似乎无法弄清楚为什么:/。控制台突出显示了半冒号,所以我删除了那些,但还是没有运气。

在使用switch语句之前,我已经在控制台中测试了游戏,并且看起来运行良好。

const userScore = 0;
const computerScore = 0;
const userScore_span = document.getElementById("user-score");
const computerScore_span = document.getElementById("computer-score");
const scoreBoard_div = document.querySelector(".score-board");
const result_div = document.querySelector(".result");
const rock_div = document.getElementById("r");
const paper_div = document.getElementById("p");
const scissors_div = document.getElementById("s");
const lizard_div = document.getElementById("l");
const spock_div = document.getElementById("k");

function getComputerChoice() {
    const choices = ['r', 'p', 's', 'l', 'k'];
    const randomNumber = Math.floor(Math.random() * 5);
    return choices[randomNumber];
}

function game(userChoice) {
    const computerChoice = getComputerChoice();
    switch (userChoice + computerChoice) {
        case "rs";
        case "rl";
        case "pr";
        case "pk";
        case "sp";
        case "sl";
        case "lk";
        case "lp";
        case "ks";
        case "kr";
            console.log("USER WINS");
            break;
        case "rk";
        case "rp";
        case "ps";
        case "pl";
        case "sr";
        case "sk";
        case "lr";
        case "ls";
        case "kp";
        case "kl";
            console.log("COMPUTER WINS");
            break;
        case "rr"
        case "pp"
        case "ss"
        case "ll"
        case "kk"
            console.log("Its a draw");
    }   
}

function main() {
    rock_div.addEventListener('click', function() {
    game("r")
    })

    paper_div.addEventListener('click', function() {
    game("p")
    })

    scissors_div.addEventListener('click', function() {
    game("s")
    })

    lizard_div.addEventListener('click', function() {
    game("l")
    })

    spock_div.addEventListener('click', function() {
    game("k")
    })
}

    main();

我已经尝试过搜索,但是我看到这些错误中的大多数是由于人们没有在代码中使用swicth语句引起的,所以决定自己写一篇文章。

2 个答案:

答案 0 :(得分:0)

您需要冒号,而不是分号。有关示例,请参阅文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

答案 1 :(得分:0)

case语句的switch部分必须以冒号(:)结尾。您的case中有些以分号(;)结尾,而另一些则完全没有。

例如,将case "rs";替换为case "rs":

另外,您最后的case缺少break

const userScore = 0;
const computerScore = 0;
const userScore_span = document.getElementById("user-score");
const computerScore_span = document.getElementById("computer-score");
const scoreBoard_div = document.querySelector(".score-board");
const result_div = document.querySelector(".result");
const rock_div = document.getElementById("r");
const paper_div = document.getElementById("p");
const scissors_div = document.getElementById("s");
const lizard_div = document.getElementById("l");
const spock_div = document.getElementById("k");

function getComputerChoice() {
    const choices = ['r', 'p', 's', 'l', 'k'];
    const randomNumber = Math.floor(Math.random() * 5);
    return choices[randomNumber];
}

function game(userChoice) {
    const computerChoice = getComputerChoice();
    switch (userChoice + computerChoice) {
        case "rs":
        case "rl":
        case "pr":
        case "pk":
        case "sp":
        case "sl":
        case "lk":
        case "lp":
        case "ks":
        case "kr":
            console.log("USER WINS");
            break;
        case "rk":
        case "rp":
        case "ps":
        case "pl":
        case "sr":
        case "sk":
        case "lr":
        case "ls":
        case "kp":
        case "kl":
            console.log("COMPUTER WINS");
            break;
        case "rr":
        case "pp":
        case "ss":
        case "ll":
        case "kk":
            console.log("Its a draw");
            break;
    }   
}

function main() {
    rock_div.addEventListener('click', function() {
    game("r")
    })

    paper_div.addEventListener('click', function() {
    game("p")
    })

    scissors_div.addEventListener('click', function() {
    game("s")
    })

    lizard_div.addEventListener('click', function() {
    game("l")
    })

    spock_div.addEventListener('click', function() {
    game("k")
    })
}

    main();