我试图将if语句放在另一个if语句中,我认为我做了一些很容易修复的事情,但我无法弄清楚它是什么或在哪里。我坚持这一点,任何建议都会有所帮助。谢谢!
var player = 0;
var xvalue = 0;
var ovalue = 0;
function tic(a)
{
if(player === 0)
{
document.getElementById(a).value = 'X';
document.getElementById(a).disabled = 'true';
player = 1;
if(a === 'aa')
{
var i = 1
for(i = 1; i <= 1; i++; )
{
xvalue++;
}
}
else if(a === 'ab')
{
var i = 1
for(i = 1; i <= 2; i++; )
{
xvalue++;
}
else if(a === 'ac')
{
var i = 1
for(i = 1; i <= 4; i++; )
{
xvalue++;
}
else if(a === 'ba')
{
var i = 1
for(i = 1; i <= 8; i++; )
{
xvalue++;
}
else if(a === 'bb')
{
var i = 1
for(i = 1; i <= 16; i++; )
{
xvalue++;
}
else if(a === 'bc')
{
var i = 1
for(i = 1; i <= 32; i++; )
{
xvalue++;
}
else if(a === 'ca')
{
var i = 1
for(i = 1; i <= 64; i++; )
{
xvalue++;
}
else if(a === 'cb')
{
var i = 1
for(i = 1; i <= 128; i++; )
{
xvalue++;
}
else if(a === 'cc')
{
var i = 1
for(i = 1; i <= 256; i++; )
{
xvalue++;
}
if(xvalue === 448)
{
alert("player 1 wins!");
}
else if(xvalue === 7)
{
alert("player 1 wins!");
}
else if(xvalue === 56)
{
alert("player 1 wins!");
}
else if(xvalue === 273)
{
alert("player 1 wins!");
}
else if(xvalue === 84)
{
alert("player 1 wins!");
}
else if(xvalue === 73)
{
alert("player 1 wins!");
}
else if(xvalue === 146)
{
alert("player 1 wins!");
}
else if(xvalue === 292)
{
alert("player 1 wins!");
}
}
else
{
document.getElementById(a).value = 'O';
document.getElementById(a).disabled = 'true';
player = 0;
}
}
答案 0 :(得分:1)
您的代码充满了语法错误:
else if(a === 'ab'){
var i = 1
for(i = 1; i <= 2; i++; ){
xvalue++;
}
您正在打开else if
区块,在内部,您打开for
区块,但只关闭一个区块。它应该是:
else if(a === 'ab'){
var i = 1
for(i = 1; i <= 2; i++; ){
xvalue++;
}//<-- close for loops
}// then close else if block
嵌套if
是非常容易的,你似乎做得对,但也许你在问是否有可能将外部和外部结合起来。内心的。在那种情况下:是的:
if (player === 0 && a === 'aaa')
也完美无缺 但说实话,你真的需要多练习一下:应该避免JS全局变量:函数应该传递参数。