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

石头剪刀布游戏中判断值相等的if语句恒返回true问题求助

Hey there! Let's figure out why your Rock-Paper-Scissors game's match check is always returning true, even when the console shows different choices. Based on what you've shared, here are the most common culprits and fixes:

First, Check for the "Single Equals" Mistake

This is the #1 gotcha for this kind of issue. If you accidentally used a single equals sign (=) instead of a comparison operator (== or ===) in your if statement, you're doing an assignment instead of checking equality.

For example:

// ❌ Wrong: This assigns computer's choice to player's choice, and the expression always evaluates to true (since non-empty strings are truthy)
if (player() = computer()) {
  console.log("Match!");
}

// ✅ Correct: Uses strict equality to compare the actual values
if (player() === computer()) {
  console.log("It's a tie!");
}

Make Sure You're Comparing the Right Values

You mentioned console.log shows the correct values, so double-check that your if statement is using the same values you're logging. A common mistake is forgetting to call your choice functions (if they're set up like your player() function):

// ❌ Wrong: Comparing the function references themselves, not their returned values
const player = function() { return "rock"; };
const computer = function() { return "paper"; };

if (player === computer) {
  console.log("Match!"); // This won't work—functions are different references
}

// ✅ Correct: Call the functions to get their actual choice values
const playerChoice = player();
const computerChoice = computer();

console.log(playerChoice, computerChoice); // Confirm these are the values you expect
if (playerChoice === computerChoice) {
  console.log("It's a tie!");
}

Verify Your Computer Choice Logic

Even if your console logs look right, double-check that your computer's choice function isn't secretly returning the same value as the player every time. For example, if you accidentally hardcoded "rock" for the computer too, that would explain the constant matches (but you said logs show different values, so this is less likely—still worth a quick check!).

Example of Working Logic

Here's a quick, fixed version of your code to reference:

// Player choice function (fixed for now—you can expand this to user input later)
function player() {
  const playerSelects = "rock";
  return playerSelects;
}

// Computer random choice function
function computer() {
  const choices = ["rock", "paper", "scissors"];
  const randomPick = Math.floor(Math.random() * choices.length);
  return choices[randomPick];
}

// Get both choices
const playerPick = player();
const computerPick = computer();

// Log to confirm values
console.log(`Player chose: ${playerPick}`);
console.log(`Computer chose: ${computerPick}`);

// Proper match check
if (playerPick === computerPick) {
  console.log("It's a tie!");
} else {
  // Add your win/loss logic here
}

If you're still stuck, share the full code for your computer choice logic and your if statement—we can dig deeper!

内容的提问来源于stack exchange,提问作者Cool Spot

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:00:27