当我单击任意2个单选按钮时,文本会重叠。我希望if else条件分开运行。这意味着当我单击更高的单选按钮时,它会检查playertotal是否高于computertotal并写入结果。与下方单选按钮相同。
这属于较长的代码:
function button1()
{
// Checks if it's the players turn
if(playerturn==true)
{
// Generates eyes for the dice
var eyes3=Math.floor(Math.random()*6)+ 1;
var eyes4=Math.floor(Math.random()*6)+ 1;
//Shows the eyes
showEyes(eyes3, 325);
showEyes(eyes4, 450);
computerturn=true;
playerturn=false;
playerTotal = eyes3+eyes4;
//Checks if player has won
if (higher=1 && playerTotal > computerTotal)
{
pen.font = '20pt Calibri';
pen.fillText('You have won',300,250);
}
else if (higher=1 && playerTotal < computerTotal)
{
pen.font = '20pt Calibri';
pen.fillText('You have lost',300,250);
}
if (lower=1 && playerTotal < computerTotal)
{
pen.font = '20pt Calibri';
pen.fillText('You have won!',300,250);
}
else if (lower=1 && playerTotal > computerTotal)
{
pen.font = '20pt Calibri';
pen.fillText('You have lost',300,250);
}
}
}
function higherRadioButton()
{
higher = 1
lower = 0
}
function lowerRadioButton()
{
lower = 1
higher = 0
}
谢谢。
答案 0 :(得分:0)
在JavaScript中,比较运算符为==
,而不是=
。您的代码应如下所示:
if (higher == 1 && playerTotal > computerTotal)
{
//...
}
所有其他陈述都一样。
为了避免将来出现此类问题,请始终将数字放在左侧:
if (1 == higher && playerTotal > computerTotal)
{
//...
}
这样一来,如果你忘了一个“=”,你就会得到错误而不是错误的结果。