You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript石头剪刀布游戏gameRound条件语句异常问题

石头剪刀布程序问题修复方案

问题1:大小写不匹配导致条件判断失效

getComputerChoice函数返回的字符串存在大小写不一致(比如"Rock"),但gameRound里的判断用的是全小写字符串(比如"rock"),严格相等运算符===会因为大小写不同判定不相等,导致所有匹配条件都不触发。

修复:将getComputerChoice的返回值统一改为全小写,同时对用户输入做标准化处理,避免大小写问题。


问题2:getComputerChoice逻辑错误,"paper"永远不会被返回

Math.random()返回0到1之间的浮点数,等于0的概率无限趋近于0,所以else if(compChoice == 0)分支永远不会执行;同时当compChoice等于0.5时,没有对应的分支处理,会导致函数返回undefined。

修复:调整区间划分,确保三个选项的概率均等:

const getComputerChoice = () => {
    let compChoice = Math.random();
    if(compChoice < 1/3) {
        return "rock";
    } else if(compChoice < 2/3) {
        return "paper";
    } else {
        return "scissors";
    }
}

问题3:game函数调用gameRound时未传递参数

game里调用gameRound()时没有传入playerSelection和computerSelection参数,导致函数内这两个参数为undefined,所有条件判断都不成立,直接进入else分支。另外原for循环中第一次迭代就return,只会执行一轮游戏,不符合5轮的需求。

修复:调用gameRound时传入参数,同时调整game函数逻辑,收集每轮结果:

const game = () => {
    let results = [];
    for(let i = 0; i < 5; i++) {
        // 每轮重新获取用户输入和电脑选择
        let playerSelection = prompt("请输入rock、paper或scissors").trim().toLowerCase();
        let computerSelection = getComputerChoice();
        results.push(gameRound(playerSelection, computerSelection));
    }
    return results;
}

问题4:用户输入未标准化

用户可能输入带空格、大小写混合的内容(比如" Rock "、"PAPER"),导致匹配失败。

修复:对用户输入做去空格和转小写处理:

let playerSelection = prompt("请输入rock、paper或scissors").trim().toLowerCase();

完整修复后的代码

// 用于确定电脑选择的函数
const getComputerChoice = () => {
    let compChoice = Math.random();
    if(compChoice < 1/3) {
        return "rock";
    } else if(compChoice < 2/3) {
        return "paper";
    } else {
        return "scissors";
    }
}

// 判定胜负的逻辑
const gameRound = (playerSelection, computerSelection) => {
    if(playerSelection === "rock" && computerSelection === "rock" || 
       playerSelection === "paper" && computerSelection === "paper" || 
       playerSelection === "scissors" && computerSelection === "scissors") {
            return "It's a tie!!!";
    }  else if(playerSelection === "rock" && computerSelection === "paper")  {
            return "Computer wins!!!";
    }  else if(playerSelection === "rock" && computerSelection === "scissors")  {
            return "You win!!!";
    }  else if(playerSelection === "paper" && computerSelection === "scissors")  {
            return "Computer wins!!!";
    }  else if(playerSelection === "paper" && computerSelection === "rock")  {
            return "You win!!!";
    }  else if(playerSelection === "scissors" && computerSelection === "rock")  {
            return "Computer wins!!!";
    }  else if(playerSelection === "scissors" && computerSelection === "paper")  {
            return "You win!!!";
    }  else {
        return "Invalid input, please enter rock, paper or scissors";
    }
}

// 启动游戏的函数
const game = () => {
    let results = [];
    for(let i = 0; i < 5; i++) {
        let playerSelection = prompt("请输入rock、paper或scissors").trim().toLowerCase();
        let computerSelection = getComputerChoice();
        results.push(gameRound(playerSelection, computerSelection));
    }
    return results;
}

console.log(game());

内容的提问来源于stack exchange,提问作者Amadi A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 07:36:18