0

在此代码中,每次打开 html 页面时都会生成两个随机数,并且它们在矩阵中的位置将被更改。例如,主表中的随机数是 2,8 2 在 matrix[0][1],matrix[1][7],matrix[2][4],matrix[3][0],matrix [4][6],matrix[5][3],matrix[6][8],matrix[3][0],matrix[7][5] ,matrix[8][2] .in 结果表8设置在这些位置,2设置在8的当前位置。我想重复这个,30次。

到目前为止,我有:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<body>
<script type="text/javascript">

var matrix = new Array();
matrix[0]=[1,2,3,4,5,6,7,8,9];
matrix[1]=[4,5,6,7,8,9,1,2,3];
matrix[2]=[7,8,9,1,2,3,4,5,6];

matrix[3]=[2,3,4,5,6,7,8,9,1];
matrix[4]=[5,6,7,8,9,1,2,3,4];
matrix[5]=[8,9,1,2,3,4,5,6,7];


matrix[6]=[3,4,5,6,7,8,9,1,2];
matrix[7]=[6,7,8,9,1,2,3,4,5];
matrix[8]=[9,1,2,3,4,5,6,7,8];

document.writeln('<table border="1">');
for (i = 0; i < 9; i++)
      {
    document.writeln('<tr>');
    for (j = 0; j < 9; j++) 
         document.writeln('<td>' + matrix[i][j] + '</td>');

    document.writeln('</tr>');
       }
document.writeln('</table>');


document.writeln('<table border="1">');
document.writeln("The random numbers are:");
document.writeln('<br>');
var r1 = Math.ceil(Math.random() * 9);
var r2 = Math.ceil(Math.random() * 9);
document.writeln(r1);
document.writeln(r2);
document.writeln('<br>');
document.writeln("The result table is:");
        for(i=0; i<9; i++)
           {
              document.writeln('<tr>');
             for(j=0; j<9; j++)
                {
                 if(matrix[i][j]==r1)
                   {
                     matrix[i][j]=r2;
                     document.writeln('<td>' + matrix[i][j] + '</td>');
                    }
                 else if(matrix[i][j]!=r1 && matrix[i][j]!=r2)
                    document.writeln('<td>' + matrix[i][j] + '</td>');
                 else if(matrix[i][j]==r2)
                       {
                          matrix[i][j]=r1;  
                          document.writeln('<td>' + matrix[i][j] + '</td>');
                        }

                }

              document.writeln('</tr>');
           }

   document.writeln('</table>');

</script>
</body>
</html>
4

1 回答 1

0

I would change your code slightly.

First, I'd make a method to print the table:

function printMatrix(var matrix)
{
   document.writeln('<table border="1">');
for (i = 0; i < 9; i++)
      {
    document.writeln('<tr>');
    for (j = 0; j < 9; j++) 
         document.writeln('<td>' + matrix[i][j] + '</td>');

    document.writeln('</tr>');
       }
document.writeln('</table>');
}

Then, I'd make a method for the swapping:

function sawpTwoNumbers(matrix)
{

document.writeln("The random numbers are:");
document.writeln('<br>');
var r1 = Math.ceil(Math.random() * 9);
var r2 = Math.ceil(Math.random() * 9);
document.writeln(r1);
document.writeln(r2);
document.writeln('<br>');

// This is new code:

for(int i=0; i< matrix.length; i++){
    var r1Index = matrix[i].indexOf(r1);
    var r2Index = matrix[i].indexOf(r2);

    matrix[i][r1Index] = r2;
    matrix[i][r2Index] = r1;
}

}

Next, I'd put your code in the pageLoad method:

function pageLoad()
{
var matrix = new Array();
matrix[0]=[1,2,3,4,5,6,7,8,9];
matrix[1]=[4,5,6,7,8,9,1,2,3];
matrix[2]=[7,8,9,1,2,3,4,5,6];

matrix[3]=[2,3,4,5,6,7,8,9,1];
matrix[4]=[5,6,7,8,9,1,2,3,4];
matrix[5]=[8,9,1,2,3,4,5,6,7];


matrix[6]=[3,4,5,6,7,8,9,1,2];
matrix[7]=[6,7,8,9,1,2,3,4,5];
matrix[8]=[9,1,2,3,4,5,6,7,8];

printMatrix(matrix);

// This is new code:
// It will call the swap method and the print method 30 times.
for(int i = 0; i < 30; i++)
{
    swapTwoNumbers(matrix);
    printMatrix(matrix);
}
}

Please Note: I haven't tested this code. I don't guarantee that it works. I hope it'll give you a push in the right direction though.

于 2013-05-24T13:55:15.077 回答