我正在尝试制作我的第一个石头剪刀布游戏。我在使用alert()函数时遇到问题。每当我打开文档时,我都会收到提示“做出决定!”但是当我回答提示时,没有返回说是人工智能还是人类赢得了这一轮。
const Humanity = 0;
const AI = 0;
//Create function to generate random computer choice
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
const randomChoice = Math.floor(Math.random() * choices.length);
const decision = choices[randomChoice];
return decision;
} getComputerChoice();
//play a round of game
function round() {
let computerChoice = getComputerChoice();
let playerDecision = prompt('Make your decision!', ''.toLowerCase());
if (playerDecision === 'rock' && computerChoice === 'scissors') {
Humanity += 1;
alert('Humanity Wins');
} else if (playerDecision === 'scissors' && computerChoice === 'paper') {
Humanity +=1;
alert('Humanity Wins');
} else if (playerDecision === 'paper' && computerChoice === 'rock') {
Humanity += 1;
alert('Humanity Wins');
} else if (playerDecision === 'rock' && computerChoice === 'paper') {
AI += 1;
alert('AI Wins');
} else if (playerDecision === 'scissors' && computerChoice === 'rock') {
AI += 1;
alert('AI Wins');
} else if (playerDecision === 'paper' && computerChoice === 'scissors') {
AI += 1;
alert('AI Wins');
}
} round();
I expect the alert to pop up once I have entered the prompted question.
运行您的代码时出现错误。
“对常量变量赋值。”
这是你的问题。我猜你是 JS 新手。不要声明变量 const 将它们声明为“var”或“let”
let - 在您声明的范围内可用
var - 在完整函数内部可用(如果在函数外部声明它们,则在 js 文件中可用)
const - 你不会改变的东西
在这种情况下,如果你在 J 的顶部将数组“选择”声明为 const,甚至在函数之外,那就太好了。
还有一个游戏提示,那就是缺少一个验证,抽奖!继续前进!