我正在构建一个剪刀石头布游戏 – 无法在if语句为真之后增加变量。

huangapple go评论57阅读模式
英文:

I'm building a rock, paper, scissors game - cannot make a variable increment after if statement is true

问题

以下是翻译好的代码部分:

let CPUchoice = [];
let gameResult = document.getElementById('game-result');

let yourScore = 0;
let computerScore = 0;

function computerChoice() {
    const picks = ['rock', 'paper', 'scissors'];
    CPUchoice = picks[Math.floor(Math.random() * picks.length)];
}

function rock() {
    computerChoice();
    if (CPUchoice === 'rock') {
        gameResult.textContent = "电脑选择了石头,平局!";
    } else if (CPUchoice === 'paper') {
        computerScore = computerScore + 1;
        gameResult.textContent = "电脑选择了纸,你输了!";
    } else {
        yourScore = yourScore + 1;
        gameResult.textContent = "电脑选择了剪刀,你赢了!";
    }
}

function paper() {
    computerChoice();
    if (CPUchoice === 'paper') {
        gameResult.textContent = "电脑选择了纸,平局!";
    } else if (CPUchoice === 'scissors') {
        gameResult.textContent = "电脑选择了剪刀,你输了!";
        computerScore = computerScore + 1;
    } else {
        gameResult.textContent = "电脑选择了石头,你赢了!";
        yourScore = yourScore + 1;
    }
}

function scissors() {
    computerChoice();
    if (CPUchoice === 'scissors') {
        gameResult.textContent = "电脑选择了剪刀,平局!";
    } else if (CPUchoice === 'rock') {
        gameResult.textContent = "电脑选择了石头,你输了!";
        computerScore = computerScore + 1;
    } else {
        gameResult.textContent = "电脑选择了纸,你赢了!";
        yourScore = yourScore + 1;
    }

}

document.getElementById('your-score').textContent = '你的得分是... ' + yourScore;
document.getElementById('computer-score').textContent = '电脑得分是... ' + computerScore;

希望这有助于解决你的问题。

英文:

I am making rock, paper, scissors game. I can't make 'yourScore' and 'computerScore' increment after true if statements. Everything else works. I've been trying different variations for the last 2 hours.

let CPUchoice = []
let gameResult = document.getElementById('game-result');
let yourScore = 0;
let computerScore = 0;
function computerChoice() {
const picks = ['rock', 'paper', 'scissors'];
CPUchoice = picks[Math.floor(Math.random()*picks.length)];
}
function rock() {
computerChoice();
if (CPUchoice === 'rock') {
gameResult.textContent = "Computer chose rock, It's a draw!";
} else if (CPUchoice === 'paper') {
computerScore = computerScore++;
gameResult.textContent = "Computer chose paper, you lost!";
} else {
yourScore = yourScore++;
gameResult.textContent = "Computer chose scissors, you won!";
}
}
function paper() {
computerChoice();
if (CPUchoice === 'paper') {
gameResult.textContent = "Computer chose paper, It's a draw!";
} else if (CPUchoice === 'scissors') {
gameResult.textContent = "Computer chose scissors, you lost!"
computerScore++;
} else {
gameResult.textContent = "Computer chose rock, you won!"
yourScore ++;
}
}
function scissors() {
computerChoice();
if (CPUchoice === 'scissors') {
gameResult.textContent = "Computer chose scissors, It's a draw!";
} else if (CPUchoice === 'rock') {
gameResult.textContent = "Computer chose rock, you lost!"
computerScore++;
} else {
gameResult.textContent = "Computer chose paper, you won!"
yourScore ++;
}
}
document.getElementById('your-score').textContent = 'Your score is... ' + yourScore;
document.getElementById('computer-score').textContent = 'Computer score is ....' + computerScore;

I have tried many variations of putting the .textContent method in the beginning, in the end. In the code there are 2 versions of incrementing the variable.

答案1

得分: 1

代码中的最后两行(document.getElementById...)只在脚本加载到浏览器时执行一次。您应该在分数发生更改时更新textContent。这里的=符号表示一次性赋值:将右侧的值分配给左侧的变量。这并不意味着从那时起两侧的值相等(有时这可能有效,但那是因为引用。参见此帖了解引用和原始值之间的区别)。

最简单的修复方法是将它们放在一个函数中:

function updateScores() {
  document.getElementById('your-score').textContent = 'Your score is... ' + yourScore;
  document.getElementById('computer-score').textContent = 'Computer score is ....' + computerScore;
}

... 并在增加computerScoreyourScore时执行此函数。

其他修复方法包括使用响应式编程库,甚至使用现代的 Web 框架,但对于这个简单的游戏来说,这些都太过于繁琐。

其次,score = score++ 基本上是一个无操作。score++ 等同于:

function incrementScore() {
  const oldVal = score;
  score++;
  return oldVal;
}

score = score++ 意味着将 oldVal 再次赋给 score。修复方法是将这些行更改为 score++(消除 score = )。

英文:

First, the last two lines (document.getElementById...) of your code is only executed once, i.e. when the script itself is loaded into the browser. What you should do is to update the textContents whenever the scores change. The = sign here means a one-time assignment: assign the right hand side value to the left hand side variable. It doesn't mean the values on both sides are equal from thereon (sometimes this can be effectively true, but that's because of references. See this post about differences between a reference and a primitive value).

The simpliest fix is to put them in a function:

function updateScores() {
  document.getElementById('your-score').textContent = 'Your score is... ' + yourScore;
  document.getElementById('computer-score').textContent = 'Computer score is ....' + computerScore;
}

... and execute this function whenever you increment computerScore and yourScore.

Other fixes include using reactive programming libraries and even using modern web frameworks, but those are too much of an overkill for this simple game.

Second, score = score++ is basically a no-op. score++ is equivalent to:

function incrementScore() {
  const oldVal = score;
  score++;
  return oldVal;
}

And score = score++ means you're assigning the oldVal to score again. The fix is to change those lines to score++ (eliminating score = ).

答案2

得分: 0

num++ 返回 num 的值,然后增加这个数字,所以你的赋值得到的是 "旧" 值。

你可以使用 ++num 来先增加,然后再赋值。或者直接加上 1

但是等一下,情节反转!因为 ++ 运算符改变它的操作数,所以赋值本身是不必要的。你可以简单地使用 num++

英文:

num++ returns the value of num, and then increments the number, so your assignment gets the "old" value.

You can use ++num to increment first and then assign. Or just add 1.

But wait, plot twist! Because the ++ operator mutates its operand, the assignment itself is unnecessary. You can simply do num++.

huangapple
  • 本文由 发表于 2023年2月19日 03:42:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定