Javascript 两个值在动作时不会按假设的那样递增

Javascript 两个值在动作时不会按假设的那样递增,javascript,Javascript,我用html、css和js创建了一个石头剪刀游戏。所有函数和元素都是在js中创建的。所以每当我按摇滚乐时,它会增加playerScore,当我按纸张时,它会增加computerscore。剪刀不会增加它们中的任何一个。我检查了功能,但没有发现任何问题。有人能告诉我为什么会这样吗 让计算机评分=0; 让playerScore=0; //----计算机播放和rand int------// 函数getRndInteger(最小值、最大值){ 返回Math.floor(Math.random()*(

我用html、css和js创建了一个石头剪刀游戏。所有函数和元素都是在js中创建的。所以每当我按摇滚乐时,它会增加playerScore,当我按纸张时,它会增加computerscore。剪刀不会增加它们中的任何一个。我检查了功能,但没有发现任何问题。有人能告诉我为什么会这样吗

让计算机评分=0;
让playerScore=0;
//----计算机播放和rand int------//
函数getRndInteger(最小值、最大值){
返回Math.floor(Math.random()*(max-min+1))+min;
}
函数computerPlay(){
常数x=getRndInteger(1,3);
如果(x==1){
返回“岩石”;
}else如果(x==2){
返回“文件”;
}否则{
返回“剪刀”;
}
}
const computerChoice=computerPlay();
功能playRound(计算机选择、播放器选择){
if(computerChoice==“rock”和playerChoice==“剪刀”){
返回1;
}否则如果(计算机选择==“剪刀”和玩家选择==“纸”){
返回1;
}else if(计算机选择==“纸张”&&playerChoice==“摇滚乐”){
返回1;
}否则如果(playerChoice==“rock”和&computerChoice==“剪刀”){
返回0;
}否则如果(playerChoice==“剪刀”和&computerChoice==“纸”){
返回0;
}否则如果(playerChoice==“纸张”和&computerChoice==“岩石”){
返回0;
}否则{
返回2;
}
}
//----玩家和计算机决策----//
函数chooservock(){
playerChoice=“岩石”;
如果(playRound(计算机选择,'rock')==1){
计算机评分++;
}else if(playRound(计算机选择,'rock')==0){
playerScore++;
}否则{
console.log();
}
展示记分板();
}
函数选择器(){
playerChoice=“纸张”;
如果(playRound(计算机选择,'纸张')==1){
计算机评分++;
}else if(playRound(计算机选择,'纸张')==0){
playerScore++;
}否则{
console.log();
}
展示记分板();
}
函数选择器选择器(){
playerChoice=“剪刀”;
如果(playRound(计算机选择,'剪刀')==1){
计算机评分++;
}else if(playRound(计算机选择,'剪刀')==0){
playerScore++;
}否则{
console.log();
}
展示记分板();
}
//--------添加按钮-----------//
const buttonContainer=document.getElementById('buttonContainer');
让rockBtn=document.createElement('button');
让paperBtn=document.createElement('button');
让scissorsBtn=document.createElement('button');
让scoreboard=document.createElement('div');
rockBtn.addEventListener('click',e=>chooservock());
paperBtn.addEventListener('click',e=>chooseper());
scissorsBtn.addEventListener('click',e=>ChooseScisors());
rockBtn.innerHTML='rock';
paperBtn.innerHTML='paper';
scissorsBtn.innerHTML='scissors';
函数显示记分板(){
如果(计算机分数==5){
计算机评分=0;
playerScore=0;
scoreboard.innerHTML='计算机获胜!';
}否则如果(playerScore==5){
计算机评分=0;
playerScore=0;
scoreboard.innerHTML='Player wins!';
}否则{
scoreboard.innerHTML=computerScore+“”+playerScore;
}
}
buttonContainer.appendChild(rockBtn).className='buttonlist';
buttonContainer.appendChild(paperBtn.className='buttonlist';
buttonContainer.appendChild(scissorsBtn).className='buttonlist';
按钮容器。附件(记分板)

选择您的行动


问题在于方法的调用:const computerChoice=computerPlay(); 只叫过一次。加载页面时。只要不刷新页面,该值将保持不变

以下是一个解决方案:

让计算机评分=0;
让playerScore=0;
//----计算机播放和rand int------//
函数getRndInteger(最小值、最大值){
返回Math.floor(Math.random()*(max-min+1))+min;
}
函数computerPlay(){
常数x=getRndInteger(1,3);
console.log('x:',x);
如果(x==1){
返回“岩石”;
}else如果(x==2){
返回“文件”;
}否则{
返回“剪刀”;
}
}
功能playRound(计算机选择、播放器选择){
if(computerChoice==“rock”和playerChoice==“剪刀”){
返回1;
}否则如果(计算机选择==“剪刀”和玩家选择==“纸”){
返回1;
}else if(计算机选择==“纸张”&&playerChoice==“摇滚乐”){
返回1;
}否则如果(playerChoice==“rock”和&computerChoice==“剪刀”){
返回0;
}否则如果(playerChoice==“剪刀”和&computerChoice==“纸”){
返回0;
}否则如果(playerChoice==“纸张”和&computerChoice==“岩石”){
返回0;
}否则{
返回2;
}
}
//----玩家和计算机决策----//
函数chooservock(){
playerChoice=“岩石”;
让计算机选择=计算机播放();
如果(playRound(计算机选择,'rock')==1){
计算机评分++;
}else if(playRound(计算机选择,'rock')==0){
playerScore++;
}否则{
console.log();
}
展示记分板();
}
函数选择器(){
playerChoice=“纸张”;
让计算机选择=计算机播放();
如果(playRound(计算机选择,'纸张')==1){
计算机评分++;
}else if(playRound(计算机选择,'纸张')==0){
playerScore++;
}否则{
console.log();
}
展示记分板();
}
函数选择器选择器(){
playerChoice=“剪刀”;
让计算机选择=计算机播放();
如果(playRound(计算机选择,'剪刀')==1){
计算机评分++;
}else if(playRound(计算机选择,'剪刀')==0){
playerScore++;
}否则{
console.log();
}
展示记分板();
}
//--------添加按钮-----------//
const buttonContainer=document.getElementById('buttonContainer');
让rockBtn=document.createElement('button');
让paperBtn=document.createElement('button');
让scissorsBtn=document.createElement('button');
让scoreboard=document.createElement('div');
rockBtn.addEventListener('click',e=>chooservock());
paperBtn.addEventListener('click',e=>chooseper());
剪刀广告