-1

I need to create a method that takes a 2d array as an argument to find out what numbers are divisible by 4 or 7. The array of integers are 1- 100 in a 2d array in c#.

this is what i got

public void DivisbleBy4And7(int[,] pNumberMatrix)
{
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            if (pNumberMatrix[i, j] % 4 || pNumberMatrix[i,j] % 7)
            {
                Console.WriteLine(pNumberMatrix[i,j];
            }
            else
            {
                Console.WriteLine("");
            }
        }
    }      
}
4

2 回答 2

3

你已经很接近了。

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {

这仅适用于 10x10 阵列,请考虑

    for (int i = 0; i < pNumberMatrix.GetLength(0); i++)
    {
        for (int j = 0; j < pNumberMatrix.GetLength(1); j++)
        {

C# 不会从intto转换bool,而且除法性要求%产生0. 所以

        // if (pNumberMatrix[i, j] % 4 || pNumberMatrix[i,j] % 7) 
           if ((pNumberMatrix[i, j] % 4) == 0 || (pNumberMatrix[i,j] % 7) == 0)
            {
于 2012-12-05T19:04:21.883 回答
2

可能你必须写:

public void DivisbleBy4And7(int[,] pNumberMatrix)
{
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            if (pNumberMatrix[i, j] % 4 == 0 || pNumberMatrix[i,j] % 7 == 0)
            {
                Console.WriteLine(pNumberMatrix[i,j];
            }
            else
            {
                Console.WriteLine("");
            }
        }
    }      
}``
于 2012-12-05T19:01:48.340 回答