简体   繁体   English

为什么我的2D数组不返回true

[英]Why doesn't my 2D array return true

Thought I was doing well with this but when checking values other than 1's, 2's etc. the output returns false when I know it should be returning true. 以为我在此方面做得很好,但是当检查1,2等以外的值时,当我知道它应该返回true时,输出将返回false。 There are numerous 'magic square' questions here in SO but i'm on my own learning and many of the answers to other questions are too advanced for me as of yet. 在SO中有很多“魔方”问题,但是我是在自己的学习中,到目前为止,其他问题的很多答案对我来说还是太高级了。

Tried several ways to code this 3x3 table I've created have been unsuccessful to find a solution. 尝试了几种方法对我创建的3x3表进行编码,但未能成功找到解决方案。 If it is something simple please excuse my beginner questions. 如果很简单,请原谅我的问题。

Revised my code however TRUE is returned when I know that it should be false. 修改了我的代码,但是当我知道它应该为假时,返回TRUE。 I've reset the sums back to zero as the loops iterate through which should be comparing the sums of individual rows and cols with each other and the diagonals. 在循环迭代时,我将总和重置为零,通过该循环应该比较各个行和列的总和彼此以及对角线。 Hopefully someone can point me in the right direction. 希望有人可以指出正确的方向。

updated Code: 更新的代码:

<p><input type="button" onClick="simpleSquare()" value="Enter a Positive 
Number"></p>
<!--create table here-->
<div id="squareTable">
<table id="magicSquare">
<tr>
<td><span id="cell00"></span>&nbsp;</td>
<td><span id="cell01"></span>&nbsp;</td>
<td><span id="cell02"></span>&nbsp;</td>
</tr><tr>
<td><span id="cell10"></span>&nbsp;</td>
<td><span id="cell11"></span>&nbsp;</td>
<td><span id="cell12"></span>&nbsp;</td>
</tr><tr>
<td><span id="cell20"></span>&nbsp;</td>
<td><span id="cell21"></span>&nbsp;</td>
<td><span id="cell22"></span>&nbsp;</td>
</tr><tr>
<td colspan="3" id="button"><p><input type="button"   
onclick="checkMagic()" value="Check if it is a magic Square"></p></td> 
</tr><tr>
<td id="output">Result:</td><td colspan="2" id="magic">&nbsp;</td></tr>

</table></div>

function simpleSquare() {
myArray = new Array([document.getElementById("cell00"), document.getElementById("cell01"),      
                    document.getElementById("cell02")], [document.getElementById("cell10"), document.
                    getElementById("cell11"), document.getElementById("cell12")],[document.getElementById("cell20"  
                    ), document.getElementById("cell21"), document.getElementById("cell22")]);
for(var rows = 0; rows < 3; rows++)
{
    for(var cols = 0; cols < 3; cols++)
    {
        var nums = parseInt(prompt("Enter a number for row " + (rows + 1) + ", column " + (cols + 1) + "."));
        myArray[rows][cols].innerHTML = nums;
    }
}

} }

function checkMagic() {
var flag = true;
var flag = true;
var rowSum = 0;
var colSum = 0;

//get sum of diagonals in magic square
var sumDiag1 = parseInt(myArray[0][0].innerHTML) + parseInt(myArray[1]
[1].innerHTML) + parseInt(myArray[2][2].innerHTML);

var sumDiag2 = parseInt(myArray[2][0].innerHTML) + parseInt(myArray[1]
[1].innerHTML) + parseInt(myArray[0][2].innerHTML);
if(sumDiag1 != sumDiag2)
    flag = false;       
//sum of rows using for loop
for(j = 0; j < 3; j++)
{
    for(i = 0; i < 3; i++)
    {
        rowSum = rowSum + myArray[i][j];
        if((rowSum != sumDiag1) || (rowSum != colSum))
        flag == false;

    }
    rowSum = 0; //reset rowsum before each iteration
}
//sum of columns
for(i = 0; i < 3; i++)
{
    for(j = 0; j < 3; j++)
    {
        colSum = colSum + myArray[i][j];
        if((colSum != sumDiag1) || (colSum != rowSum))
        flag == false;
    }
    colSum = 0; //reset colsum before each iteration
        flag == false;
}

//output to user if magic square        
if(flag == false)
    document.getElementById("magic").innerHTML=("Sorry, your square is 
not a magic square");

if(flag == true)
    document.getElementById("magic").innerHTML=("Wow! This is a magic   

square"); 广场”);

} }

Alright… your array contains table cells, so you should always read their value with myArray[i][j].textContent (or innerHTML if you prefer). 好吧……您的数组包含表单元格,因此您应该始终使用myArray[i][j].textContent (或您愿意的是innerHTML)读取它们的值。 That'll fix one problem. 这样可以解决一个问题。

Next, their value is a string. 接下来,它们的值是一个字符串。 No point in trying to convert when you get the input, because text inside the DOM is always strings. 获得输入后尝试进行转换没有任何意义,因为DOM中的文本始终是字符串。 So when you read, you get a string, and that changes the semantics of + to concatenation instead of addition. 因此,当您阅读时,会得到一个字符串,并将+的语义更改为串联而不是加法。 So actually you should read with +myArray[i][j].textContent (using + as a unary operator for converting to Number). 因此,实际上您应该使用+myArray[i][j].textContent (使用+作为转换为Number的一元运算符)。

Finally, as Barmar pointed out, you should check each row and column separately, not sum them all up (ie initialize sum to zero on each iteration of the first loop, and just before the iteration ends, compare with the diagonal sum). 最后,正如Barmar指出的那样,您应该分别检查每一行和每一列,而不是将它们全部累加(即,在第一个循环的每次迭代中将总和初始化为零,并且在迭代结束之前,将其与对角线总和进行比较)。

Pretty messy and inefficient code too, but we're not on CodeReview… 代码也很混乱且效率低下,但是我们不在CodeReview上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM