Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何防止控制台在JavaScript游戏中打印?_Javascript - Fatal编程技术网

如何防止控制台在JavaScript游戏中打印?

如何防止控制台在JavaScript游戏中打印?,javascript,Javascript,我一直在练习一个使用JavaScript的游戏,想知道当用户做出定义错误的选择时,如何防止控制台打印 代码如下: var user = prompt("Do you choose rock, paper or scissors?"); var computer = Math.random(); if (computer < 0.34) { computer = "rock"; } else if (computer <= 0.67) { computer = "pap

我一直在练习一个使用JavaScript的游戏,想知道当用户做出定义错误的选择时,如何防止控制台打印

代码如下:

var user = prompt("Do you choose rock, paper or scissors?");
var computer = Math.random();
if (computer < 0.34) {
    computer = "rock";
}
else if (computer <= 0.67) {
    computer = "paper";
}
else {
    computer = "scissors";
}
console.log("Computer Chooses: " + computer);
console.log("User Chooses: " + user);
var compare = function (computer, user) {
    if (computer === "rock") {
        if (user === "scissors") {
            return "Computer wins by choosing rock!";
        }
    }
    else if (computer === "scissors") {
        if (user === "paper") {
            return "Computer wins by choosing scissors!";
        }
    }
    else if (computer === "paper") {
        if (user === "rock") {
            return "Computer wins by choosing paper!"
        }
    }
    if (computer === user) {
        return ("It is a tie!")
    }
    else if (user === "paper") {
        if (computer === "rock") {
            return ("You win by choosing paper!")
        }
    }
    else if (user === "rock") {
        if (computer === "scissors") {
            return ("You win by choosing scissors!")
        }
    }
    else if (user === "scissors") {
        if (computer === "paper") {
            return ("You win by choosing scissors!")
        }
    }
    ***if (user !== "rock" && user !== "paper" && user !== "scissors") {
        confirm(user + " is an invalid entry.");
    }***
};
compare(computer, user);
最后,我截取了一段代码,这段代码向用户显示他输入了错误的字符。我想知道的是:


一旦有人输入了错误的输入,我如何防止任何东西显示在控制台上?

在开始时验证用户输入。如果用户通过,则仅显示代码,否则显示错误

var user = prompt("Do you choose rock, paper or scissors?");
if(user !== "rock" && user !== "paper" && user!== "scissors") { 
    confirm(user + " is an invalid entry."); 
} else {
    // code for process
}

一种选择是不断向用户询问有效输入,直到给出有效输入:

while (user != "rock" && user != "paper" && user != "scissors") {
    user = prompt("Do you choose rock, paper or scissors?")
    if (user == null) {
        break;
    }
};

if (user != null) {
...
}

您可以将此代码添加到js代码之上

var console = {};
console.log  = function(){};
你的if语句比必要的多;你可以简化逻辑

var getComputerMove = function () {
    // equivalent of Math.floor
    // aka it grabs the integer component of `Math.random() * 3`
    // either 0, 1, or 2
    return (Math.random() * 3) | 0;
}

var playRockPaperScissors = function () {
    var moves = [ "rock", "paper", "scissors" ]
      , user = ""
      , computer = getComputerMove();

    // while `user` is not in the `moves` array
    // `Array.indexOf` returns -1 if an element is not in the array
    // prompt the user for his move
    while (moves.indexOf(user.toLowerCase()) == -1) {
        user = prompt("Rock, paper, or scissors?");
    }

    // this is where you can save yourself all that typing and think out
    // the possible movesets:
    // item:    rock < paper < scissors < rock
    // index:   0    < 1     < 2        < 0
    // so, the computer wins if its choice is greater than the user's,
    // or if the computer chose "rock" and the user chose scissors
    // we can translate this to
    // user < computer || (computer == 0 && user == 2)


    var userIndex = moves.indexOf(user.toLowerCase());

    // uncomment, if you want to see the moves
    // console.log("user:", user, "computer:", moves[computer]);

    if (userIndex < computer || (userIndex == 2 && computer == 0) {
        alert("Computer wins!");
    } else {
        alert("User wins!");
    }
}

这是可行的,但是我想再次请求用户输入。看起来Danilo回答了该怎么做。你没有在问题中指定。尝试后,我注意到它不再提示用户输入,所以我想问你如何向用户重复该问题,但Danilo已经回答了该怎么做。Danilo,这很好,但是,如果用户点击“取消”,并希望阻止该框反复发送相同的问题,该怎么办?我更新了条件以满足您的要求。请注意,您还收到了其他一些很好的答案,例如royhowie的答案,它可能会帮助您重新审视您的代码,并熟悉一些新概念,比如Howie的一些东西。1如果用户没有在索引0、1和2之间进行选择,索引如何自动转到-1?2此代码不会提醒用户计算机选择了什么。假设我选择了纸,我如何提醒用户电脑选择了剪刀,而不是只显示电脑赢了?用户最初没有给出任何输入user==,因此`moves.indexOfuser.toLowerCase==-1,因为.toLowerCase`不在moves中要显示计算机的移动,请将alertmoves[computer]放在某处